Exclude Certain WordPress Categories from Being Searched

March 18, 2009 · Print This Article

You may not want all of your categories to appear in your search results.

For example, if you’ve set up a sidebar asides category, you may not want those short snippets tangling up with your results.

To remove a category, first open up your theme’s functions.php file (Or create a blank file with that name if your theme doesn’t have one);

Then paste the following code at the end:

<?php
function SearchFilter($query) {
if ($query->is_search) {
$query->set('cat','8,16');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
?>

Simply change the “8,16” part to a comma-separated list of the category IDs for any category you want to exclude (Can get the category IDs from WordPress by going to Posts > Categories).


Your choice for site templates and wordpress themes

Comments

2 Responses to “Exclude Certain WordPress Categories from Being Searched”

  1. ricardo on April 12th, 2011 11:54 pm

    Are you sure it works? I would replace a line “$query->set(‘cat’,’8,16′);” with “$query->set(‘category__not_in’, array(8,16) );”.

  2. Remove posts/pages from wordpress search results on July 10th, 2012 8:29 am

Got something to say?

You must be logged in to post a comment.