wp query by search in titles only & put the posts in loop

If you want to limit the filter from this answer (add that function to your plugin or your theme’s functions.php) just to one query, remove the filter when you are done:

add_filter( 'posts_search', '__search_by_title_only', 500, 2 );

$query = new WP_Query(
    array(
        's'         => 'mykeyword myotherkeyword',
        'cat'       => array( 22, 32 ),
        'orders'    => 'DESC',
        'showposts' => 6
    )
);

remove_filter( 'posts_search', '__search_by_title_only', 500 );

if ( $query->have_posts() ) :
    while ( $query->have_posts() ) :
        $query->the_post();
        the_title( '<p>', '</p>' );
    endwhile;

    wp_reset_postdata();
endif;

Leave a Comment