Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

up vote 4 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

 
discard

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.