Post Pic

How to: Exclude posts or pages from search results

Would you like to be able to control which post or pages must be exclued from searches on your WordPress blog? If yes, this recipe will probably help you a lot. Just read on.

To achieve this recipe, simply paste the following code on the functions.php file from your theme. In this example, posts with IDs 8 and 15 will be excluded from your blog’s search results:

function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('cat','8,15');
    }
    return $query;
}

add_filter('pre_get_posts','SearchFilter');

Credits goes to Kyle Eslick for this awesome hack!

15 Responses

Dec 22 2008 13:49

This is great! Excluding pages from search is a must in my case because pages ruins the display of some themes when it gets displayed in internal results.

Dec 22 2008 18:51

Great tip to keep in mind ;)

Dec 22 2008 19:01

Awesome hack, indeed.
REgards
Shane
twitter:shanearthur

Dec 27 2008 22:42

Wow… this is great when you have some posts that you want to be more… hidden [not private].

I think something like this could be implemented in a future version of WP :-)

Mar 31 2009 19:29

what about excluding categories from results?

Jul 16 2009 17:28

This code has removed all pages from search results. I am using WordPress 2.8.1, is there an updated version of this code?

Jul 16 2009 19:51

@Chet: Weird problem. The code was made before 2.8 was released, so maybe it is the problem. I’ll have a look when I’ll have some time.

Sep 16 2009 12:21

Hi,
i use
$query->set(‘cat’,'-8,-15′);
for my WP 2.8.4 and it works fine when you don’t want to show the results for cat8 and cat15.

Oct 22 2009 11:57

The problem with this is a error that show up on Search Results when you find a image on media inside the dashboard :(

Mar 30 2010 12:13

I guess with the updates the code doesn’t work anymore, but here is a new code snippet that does the trick, which I tested of course and it works perfectly

function vibeExcludePages($query) {
if ($query->is_search) {
$query->set(‘post_type’, ‘post’);
}
return $query;
}
add_filter(‘pre_get_posts’,'vibeExcludePages’);

May 07 2010 06:03

I am trying to exclude a certain page from the search query. This hack actually only includes this page. Also, I couldn’t find the filter “pre_get_posts” in the WordPress documentation. Others have stated that this hack doesn’t work for newer WordPress installations (I am using 2.9.2), maybe it’s because this filter call isn’t part of WordPress anymore?

May 12 2010 23:03

I looked around for a solution but could not find a good solution on how to remove a page from the search results. In the end I came up with this code:

ID == ‘Page ID Goes Here’) continue; ?>

Just swap the above code with the code found the search.php inside your themes folder:

If you want to skip multiple pages go with:
ID == ’00′ || $post->ID == ’01′|| $post->ID == ’02′) continue; ?>

May 12 2010 23:07

Sorry about that the code copied over funny.

while (have_posts()) : the_post(); if($post->ID == ‘Page ID Goes Here’) continue;

Just swap the above code with the code found the search.php inside your themes folder:

while (have_posts()) : the_post();

If you want to skip multiple pages go with:
while (have_posts()) : the_post(); if($post->ID == ’00′ || $post->ID == ’01′|| $post->ID == ’02′) continue;

php while (have_posts()) : the_post();

If you want to skip multiple pages go with:while (have_posts()) : the_post(); if($post->ID == ’00′ || $post->ID == ’01′|| $post->ID == ’02′) continue;

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required