• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
SnippetPress

SnippetPress

The Home of WordPress Snippets

Vultr Web Hosting
  • Cheat Sheets
  • Reviews
  • Snippets
  • Tutorials

How to Exclude Specific Tags and Categories from WordPress Search Results

June 15, 2020 by SnippetPress Leave a Comment

Sometimes there may be certain categories or tags that you don’t want to appear in your WordPress search results. You could use a plugin to exclude posts that are in these categories or tags, or you could just add a little snippet to your site.

Excluding Categories

To exclude posts that are in a specific category from your WordPress search, simply use the following snippet:

add_filter( 'pre_get_posts', 'snippetpress_search_exclude_categories' )
function snippetpress_search_exclude_categories( $query ) {
	if ( $query->is_search && !is_admin() ) {
		$query->set( 'cat','-5, -10, -12' );
	}
	return $query;
}

In the above snippet, any posts that are in categories 5, 10 or 12 will not show in the search results. Just replace these numbers with the IDs of the categories you want to exclude. You can have as many or as few categories as you need.

Excluding Tags

To exclude posts that have a specific tag from your WordPress search, simply use the following snippet:

add_filter( 'pre_get_posts', 'snippetpress_search_exclude_tags' )
function snippetpress_search_exclude_tags( $query ) {
	if ( $query->is_search && !is_admin() ) {
		$query->set( 'tag','-2, -9, -11' );
	}
	return $query;
}

In the above snippet, any posts that have tags 2, 9 or 11 will not show in the search results. Just replace these numbers with the IDs of the tags you want to exclude. You can have as many or as few tags as you need.

Where to add the snippet?

Whichever snippet you choose to use, you should place it at the bottom of the functions.php file of your child theme. Make sure you know what you’re doing when editing this file. Alternatively, you can use a plugin such as Code Snippets to add the custom code to your WordPress site. If you need further guidance on how to add the code, check out our post on How to Add WordPress Snippets.

Filed Under: Snippets, Snippets, WordPress Core

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

E-mail Newsletter

More to See

WooCommerce – Prevent Product Being Added To Cart If A Specific Product Is Already In Cart

July 15, 2020 By SnippetPress

WooCommerce – Display an “Add $X To Get Free Shipping” Notice

July 13, 2020 By SnippetPress

Footer

Recent

  • Change The WordPress Page Title Separator Without A Plugin
  • WooCommerce – Prevent Product Being Added To Cart If A Specific Product Is Already In Cart
  • WooCommerce – Display an “Add $X To Get Free Shipping” Notice
  • WooCommerce – Integrate Google Merchant Center Customer Reviews
  • WooCommerce – Disable Out of Stock Variations

Search

Copyright © 2023 ·SnippetPress