Exclude WooCommerce Categories from the Shop Page

Code Snippets Woocommerce Woocommerce Hooks WooCommerce Tips Wordpress WordPress Development WordPress Hacks WP Best Practices

Exclude WooCommerce Categories from the Shop Page Tutorial/Guide

Need to remove certain product categories from your WooCommerce shop page? Instead of deleting them, you can tweak your product queries using the woocommerce_product_query hook. This is a safe and flexible way to control product visibility on your shop page.

Steps to Exclude Product Categories

Add the following code snippet to your child theme’s functions.php file:

function custom_exclude_category_from_shop( $q ) {
    if ( is_shop() ) {
        $exclude_category = 'your-category-slug'; // Replace with your actual slug

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

        $tax_query[] = array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => array( $exclude_category ),
            'operator' => 'NOT IN',
        );

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

How It Works

The code hooks into the WooCommerce product query, excluding products that belong to a specific category. Just update the slug with the category you want hidden.

Important Notes

  • Products from excluded categories can still be accessed via direct URLs.
  • This only affects the main shop page, not individual category or search pages.
  • Use a child theme or custom plugin to safely apply this customization.

For advanced filtering options, refer to the official WooCommerce documentation.

Final Thoughts

Controlling product visibility helps streamline your shop layout. With a small code tweak, you can manage how categories appear and improve user navigation.

💡 Have a Coding Problem?

Search our archives or reach out to our team for solutions and expert advice.