Excluding WordPress Categories from Site Search

Excluding WordPress categories from site searchThere are times when you may want to exclude WordPress categories from your website search. For example you may want site visitors to be able to search the product article category but not company press releases. The code below will restrict this search. The code is entered into your functions.php file and will exclude category IDs listed. In this example I exclude category IDs 3 and 9.

If you are using a child theme you should enter this into your child theme’s function.php file. If you child theme does not have a functions.php file you can create one and put it in your child theme root folder. Click to read our article on WordPress Child Themes.

Excluding WordPress Categories Code


function SearchFilter($query) {
  if ( $query->is_search && ! is_admin() ) {
    $query->set('cat','3,9');
 }
  return $query; 
}

add_filter('pre_get_posts','SearchFilter');
,
Previous Post
Cleaning-up Post Meta Screens With Tabs

Related Posts

You must be logged in to post a comment.
Menu