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).


Related articles:

Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.