WooCommerce: Exclude Category from Search Results

We’ve already seen how to only display products from a single category on the Shop page. Today, we’ll do something similar, but we’ll target the search result.

Code is somewhat similar to the example I linked to above, so it will use once again the “pre_get_posts” filter in order to modify the query before products are returned on the screen. Enjoy!

I’ve searched for “courses” here, and because the snippet below is active, no search result (WooCommerce product) will show from the “Courses” category.

PHP Snippet: Exclude Products From a Given Category @ WooCommerce Search Result Page

 * @snippet       Exclude Cat @ WooCommerce Product Search Results
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/

add_action( 'pre_get_posts', 'bbloomer_exclude_category_woocommerce_search' );

function bbloomer_exclude_category_woocommerce_search( $q ) {
   if ( ! $q->is_main_query() ) return;
   if ( ! $q->is_search() ) return;
   if ( ! is_admin() ) {
      $q->set( 'tax_query', array( array(
         'taxonomy' => 'product_cat',
         'field' => 'slug',
         'terms' => array( 'courses' ), // change 'courses' with your cat slug/s
         'operator' => 'NOT IN'

Advanced Plugin: WooCommerce Protected Categories

The code snippet above focusses on excluding products from a particular WooCommerce category from search results. If you’re doing this then it’s likely that you’ll also need other ways to control who can access the products from each category.

I looked for a flexible no-code way of doing this, and discovered the WooCommerce Protected Categories plugin.

This plugin from Barn2 gives you full control over product visibility on a per-category basis. For each category, you can choose which users and/or roles can access it and the products within it. There’s an option to show products from restricted categories in public areas such as search results, or you can hide them from unauthorized users completely.

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Exclude Category from ‘product_categories’ Shortcode
    Sometimes solutions are very simple, and you don’t need rocket science to fix your issue! A client of mine needed to hide a category from the Product Categories Shortcode ( BloomerArmada (1) WooCommerce Mini-Plugins (30) ); in fact, there is no parameter that allows you to “exclude” a given product category such as “uncategorized” or […]
  • WooCommerce: Hide Products From Specific Category @ Shop
    A very handy snippet. Sometimes, you only want to show certain categories on the shop page, and have those products ONLY show under the category archive instead.
  • WooCommerce: “Sale” Category (Automatic)
    You can use a shortcode or block in order to display the WooCommerce products on sale. However, what if you wanted a proper “product category” called “Sale” – and where you didn’t need to manually assign this category to each product? Basically, how do we display all the discounted products in a custom category called […]
  • WooCommerce: Hide Hidden Products From Cart, Order, Emails
    While working on a workaround for a client, I had to hide a hidden product from the cart, checkout, order received and emails. Hidden products already don’t show on the shop and category pages, but they do show on the cart if they’re added to cart “programmatically”. A tough task, but as usual here I […]
  • WooCommerce: Display Product Categories @ Cart & Checkout Pages
    While working for a freelance client I had to “detect” the cart item categories in order to apply some PHP customization. So I thought – why not share with you how to display product categories in the Cart and Checkout? This adds a nice touch to those two vital pages, and prints a list of […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

4 thoughts on “WooCommerce: Exclude Category from Search Results

  1. Another masterpiece of code. Thanks man, really, God Bless you and your family.

  2. PHP Snippet: Exclude Products From a Given Category @ WooCommerce Search Result Page

    Hi – I got this error:

    Your PHP code changes were rolled back due to an error on line 66 of file wp-content/themes/dt-the7-child/functions.php. Please fix and try saving again.

    syntax error, unexpected ‘add_action’ (T_STRING)

    WordPress version: 6.1.1
    WooCommerce version: 7.1.0
    PHP version: 7.3.29

    1. What’s on line 66? Did you copy the exact snippet including comments?

Questions? Feedback? Customization? Leave your comment now!

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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