whb-logo8whb-logo8whb-logo8whb-logo8
  • Home
  • About
    • About
    • Contact
  • Blog
  • Methodology
  • Affiliate Disclosure
    • Disclaimer
    • Privacy Policy
✕

WooCommerce Part 5 – How to Exclude Products in a Specific Category from WooCommerce Search

Web Hosting Buddy here, back at it again with our WooCommerce coding series!

In today’s post, we’re going to cover something that seems to be a common question related to WooCommerce searching—how do you prevent products from a specific category from appearing in a search?

For example, let’s say that you’ve got an online clothing store that sells all different types of clothing (jeans, shoes, pants, suits, etc.), but for some reason, you don’t want any items from the t-shirt category to appear in the search results—what do you do?

Well, in this case, you’ll need a custom PHP function that adjusts the search query results just prior to runtime. And we’ve seen some other tutorials floating around the web on this topic, but the problem is that they don’t seem to work. Either the code is wrong, or it doesn’t work with the newest version of WooCommerce and WordPress.

Sound complicated? Well, it can be—but luckily we’ve written the code for you, so all you need to do is swap out our example category (t-shirts) with your own, paste this into your functions.php file, and you’re good to go!

Like always, we’ll show you the full code, and then look at each step to explain what’s happening under the hood.

Here’s the full code:

/* Remove Product Category from Search */

function remove_cat_from_search ( $q ) {

   if (!is_admin() && $q->is_main_query() && $q->is_search()) {

       $q->set( 'post_type', array( 'product' ) );
       $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 't-shirts' ), //no t-shirts in search
           'operator' => 'NOT IN'
    );

    $q->set( 'tax_query', $tax_query );
    
    }
}

add_action( 'pre_get_posts', 'remove_cat_from_search' );

Now, let’s take a look at each step…

Here’s the first part of code we’re going to look at:

/* Remove Product Category from Search */

function remove_cat_from_search ( $q ) {

   if (!is_admin() && $q->is_main_query() && $q->is_search()) {

In this snippet, we add a comment to the function so we know what it does (in case we forget later on and go back to edit the code, etc.), and then we declare the function itself named “remove_cat_from_search.”

After that, we add an if statement that checks to see if the user is not an admin (no results are removed for admins), and then we check that it’s a main query, and then that the query is for a search. If all three of these things are true, then we proceed to the next steps.

Here’s the next snippet we’re looking at:

$q->set( 'post_type', array( 'product' ) );
       $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 't-shirts' ), //no t-shirts in search
           'operator' => 'NOT IN'
    );

    $q->set( 'tax_query', $tax_query );
    
    }
}

In this code, we’re setting/getting information about the query, and then using an array to modify that query so it does not contain an items from the t-shirts category. The “NOT IN” operator is used to exclude the taxonomy with a slug field containing “t-shirts” from the query. “NOT IN” is a SQL operator that allows us to perform this exclusion.

Then, after we’ve performed the exclusion, we set the new tax_query with the updated data and close off our functions with brackets that correspond to the if statement arguments and the entire function.

Note: when you use this code, don’t forget to change the “t-shirts” category to one of your categories that you want to exclude from search, or else it won’t work.

And here’s the last piece of code we’re going to look at:

add_action( 'pre_get_posts', 'remove_cat_from_search' );

This code hooks to our function on the “pre_get_posts” action, which fires after the query variable object is created, but most importantly—before the actual query is run. This allows for a seamless experience where users will never see any difference in the speed of the search, but the products in the category you want to hide will not be there.

And that’s it! We hope you’re able to apply this code to your site, and fine-tune your WooCommerce search for users!

Related posts

May 26, 2020

How to Create a MailChimp Opt-In Checkbox for Contact Form 7 WITHOUT a WordPress Plugin


Read more

Searching for an Awesome WooCommerce Host?

Want your WooCommerce search results to be lighting fast? Check out our recommended WooCommerce hosts below to speed up your search!

Visit SiteGround

Visit WP Engine

Hey hey! Just a quick note: if you click on one of our links and buy hosting, we might get a commission/compensation. This helps us create these tutorials for free and we’d definitely appreciate the support 🙂

Guides & Reviews

  • The Best WordPress Hosting – An Expert Guide
  • The Best Web Hosting for Small Businesses
  • A Guide to Web Hosting for Nonprofits
  • The Best Domain Name Registrars
  • The Ultimate Guide to Web Hosting Alternatives
  • Web Hosting Glossary
  • Web Hosting How-Tos & Tutorials
  • Best Web Hosting & Software

Comparisons

  • The Fastest Web Hosting - Find Out Who Wins!
  • A Big List of Web Hosting Companies
  • Bluehost vs Hostgator
  • Dreamhost vs Hostgator
  • Bluehost vs Godaddy
  • Siteground vs Hostgator
  • Bluehost vs Dreamhost
  • The 5 Best GoDaddy Alternatives
  • Top 12 JMeter Alternatives
  • 20 Best Load Testing Tools
  • Hosts We Don't Recommend

Deals & Savings

  • Bluehost Coupon Codes & Discounts
  • Hostinger Coupon Codes & Deals
  • SiteGround Coupon Codes & Deals
  • DreamHost Coupon Codes & Discounts
  • HostGator Coupon Codes & Discounts
  • WPEngine Coupon Codes & Promos
  • NameSilo Coupon Codes & Deals
  • Web Hosting Coupons & Deals
  • Cheap Web Hosting
  • Cheap Email Hosting
✕
Follow @WebHostBuddy
Copyright 2025 WebHostingBuddy.com. All rights reserved.