WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to modify search results. Currently I hook to the_posts and check wp_query->is_search to determine if I am looking at the search results page.

However, I noticed that the_posts is executed not only on the search results, but also on any excerpt list on the page (such as a listing of news items in the footer of the page.)

How can I tell which the_posts call is which? Should I assume it's the 'first' one? or is there a better way?

share|improve this question
up vote 6 down vote accepted

I'd say it's perfectly fine to do it the way you're doing it now, that is

function my_the_posts($posts, $query = false) {
    if( is_search() ){
        // do your thing here
    return $posts;
add_filter( 'the_posts', 'my_the_posts' );

Don't worry about those excerpts in the footer, even though they may be part of the search page (which the footer will be, obviously), they're part of a different query and so will not pass the is_search() test.

share|improve this answer
I was checking, $wp_query->is_search() not is_search(). That's exactly what I was looking for, Thank you! – John Lee Mar 20 '13 at 22:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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