By default, WooCommerce lists all product categories on the Shop page.

However, sometimes you have a situation where you might want to exclude products of a particular category from displaying on the Shop page.

For instance, you might have created a product category for bundles or maybe for wholesale products. Therefore you want to hide these on the public shop page. However, still have the direct URL for these categories available.

How do you do it?

In order to exclude a product category, you need to add a code snippet or use a plugin like WooCommerce shortcodes Kit.

Now let’s take a look at the step-by-step process of excluding products of a particular category.

Imagine that you run an online store that sells hoodies and accessories for men.

Your products include hoodies, belts, caps, glasses, and beanies.

The shop categories can be divided into two:

  1.  Hoodies – (Hoodies with logo, hoodies with pocket, plain hoodies)
  2. Accessories (belts, caps, glasses, beanies)

Your shop page will look like this:

How to exclude a product from a category

Now you want to exclude a certain shop category from the shop page.

Let’s say you want to exclude Hoodies.

To do this, you can either add a snippet code to your child theme’s functions.php file or install the WooCommerce Shortcode plugin.

Note: Do not add the code snippet directly to your Parent Theme functions.php file as it will be removed when you update the theme.

Snippet code method

  1. Log into your WordPress dashboard
  2. Copy the snippet code below
/**
 * Exclude products from a particular category on the shop page
 */
function custom_pre_get_posts_query( $q ) {

    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'clothing' ), // Don't display products in the clothing category on the shop page.
           'operator' => 'NOT IN'
    );


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

}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

3.  On the left panel of the dashboard, go to Appearance>Theme Editor> functions.php. Remember to select the functions.php of your child theme. You should be running a child theme rather than using the parent theme. This will prevent your changes from being over written.

Or access your child theme’s functions.php via ftp.

Then add the snippet code (before “?>” if you have it) and Update the file.

Hoodies snippet code
We excluded hoodies by editing the code.

Then hoodies will be excluded from the shop page as shown below

Excluding products from a specific shop category
Hoodies excluded from the shop page

Now let’s exclude the accessories from the shop page instead of hoodies.

Again, you need to edit the code snippet as we did before.

Excluding accessories
We edited the code to exclude accessories from the shop page.

Once you make the edits, update the Child theme PHP File.

Now you can see your shop page with accessories excluded.

Guide to excluding a product from a specific category
Shop page with accessories excluded.

That’s how you exclude a specific product category from the shop page.

Did you like this article? Then give our amazing eCommerce support services a try.