
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.

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!
11 Responses
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.
Great tip to keep in mind
Awesome hack, indeed.
REgards
Shane
twitter:shanearthur
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
what about excluding categories from results?
This code has removed all pages from search results. I am using Wordpress 2.8.1, is there an updated version of this code?
@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.
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.
The problem with this is a error that show up on Search Results when you find a image on media inside the dashboard
Trackbacks: