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 6
 * @donate $9     https://businessbloomer.com/bloomer-armada/

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'

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 (25) ); in fact, there is no parameter that allows you to “exclude” a given product category such as “uncategorized” or […]
  • 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 […]
  • WooCommerce: Add a Product Search Bar in the Header/Footer
    Hola amigos, today’s snippet actually comes from my own website. You might have noticed there is a little “magnifying glass” in the navigation menu which scrolls down to a search bar. Mine, specifically, searches exclusively for blog posts (I excluded pages, products, etc.), but you can customize this and make it search for products only […]
  • WooCommerce: Edit “Add to Cart” Text by Product Category
    Today we take a look at the WooCommerce “Add to Cart” buttons. What if you wanted to change the “Add to Cart” text depending on the Product Category? For example, you may want to show “Buy Now” for books and “Add to Basket” for cds.
  • 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 […]

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

2 thoughts on “WooCommerce: Exclude Category from Search Results

  1. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you!

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