1

i need to get posts by search in posts title only not search in full post & put the posts in loop to show it in a slider

Note : i need to get posts by keyword in title & put it in loop to showing it, not limiting search in all of the site

i used wp_query to do that but it is get posts by searching in full post "content&title"

 <?php $query = new WP_Query( 's=mykeyword&cat=22,32&order=dsc&showposts=6' ); ?>
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 <p><?php the_title(); ?></p>
 <?php endwhile; ?>
 <?php endif; ?>

how can i do that

7
  • You question is self-contradicting. Please explain your problem better.
    – fuxia
    Commented Oct 26, 2013 at 13:59
  • i have updated the q i think it is very clear now
    – adnan
    Commented Oct 26, 2013 at 14:03
  • 1
    why did you delete the previous question (120165) with the same code ?
    – birgire
    Commented Oct 26, 2013 at 14:38
  • @birgire because it is marked as duplicated & this is not true
    – adnan
    Commented Oct 26, 2013 at 14:44
  • 1
    The filter callback from this question/answer should do what you need.
    – s_ha_dum
    Commented Oct 26, 2013 at 15:39

1 Answer 1

5

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;
14
  • 1
    "please post the full code with while loop & if condition like my code" - WPSE is a question and answer site, not a support forum. If you need that level of hand-holding, you might be better-served looking for paid support somewhere. Commented Oct 26, 2013 at 14:10
  • @ChipBennett thanks for your very very useful information
    – adnan
    Commented Oct 26, 2013 at 14:17
  • 1
    @adnan Yes, but you should use a different syntax for WP_Query. See my update.
    – fuxia
    Commented Oct 26, 2013 at 14:44
  • 1
    @adnan Since version 3.7, WordPress will treat two space separated words as two keywords if there not enough exact matches.
    – fuxia
    Commented Oct 26, 2013 at 14:51
  • 1
    You could try s=hello+world&sentence=1 vs s=hello+world&sentence=0, if that doesn't work try asking a new question on that problem.
    – birgire
    Commented Oct 26, 2013 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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