0

I have an SQL query here:

$data_listg = $wpdb->get_results( "SELECT ID FROM " . $wpdb->prefix . "posts where post_type='post' and SUBSTR(" . $wpdb->prefix . "posts.post_title, 1, 1) LIKE '%$data' and post_status='publish' LIMIT 2" );

The query shows posts by first word. For example:

If titles start with "A" or "B" then show that post first.

  • American hair style
  • A new Audi
  • Amazing cars
  • Alphabet

or "B"

  • B

The problem is when to change:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args  = array(
    'paged'          => $paged,
    'post_type'      => 'post',
    'post_status'    => 'publish',
    'posts_per_page' => 2
);

How do I add that into my WP_Query $args?

0

You can use custom filters in $args (not tested)

add_filter( 'posts_where', 'find_by_title_filter', 10, 2 );
function find_by_title_filter( $where, &$wp_query )
{
    global $wpdb;
    if ( $find_title_arg = $wp_query->get( 'find_title' ) ) {
        $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $wpdb->esc_like( $find_title_arg ) ) . '';
    }
    return $where;
}

$custom_var = 'a';

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
         $args = array(
                'paged'          => $paged,
                'post_type'      => 'post',
                'post_status'    => 'publish',
                'posts_per_page' => 2,
                'find_title'     => $custom_var
            );
  • ohh i see, but not working, okey i want explore your code – Ofri Jun 2 '16 at 14:19
0

yess SOLVED i modified

add_filter( 'posts_where', 'find_by_title_filter', 10, 2 );
function find_by_title_filter( $where, &$wp_query )
{
    global $wpdb;
    if ( $find_title_arg = $wp_query->get( 'find_title' ) ) {
        $where .= " AND SUBSTR(" . $wpdb->posts . ".post_title,1,1) LIKE '%$find_title_arg%' ";
    }
    return $where;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.