Excluding Product Categories from WooCommerce Shop Page

Excluding Product Categories from the WooCommerce Shop Page

 

Sometimes, you may want to prevent certain product categories from appearing on your WooCommerce shop page without deleting them or affecting their visibility elsewhere. You can achieve this easily by modifying the WooCommerce product query using the woocommerce_product_query hook. Below is a quick guide to help you exclude specific product categories from showing up on the shop page.

 

Follow the steps below to exclude a product category from the shop page:

function exclude_category_from_shop( $q ) {
    if ( is_shop() ) {
        // Replace 'category-slug-to-hide' with the actual slug of the category you want to hide
        $category_slug_to_exclude = 'category-slug-to-hide';

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

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

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

How Does This Work?

This code works by hooking into the WooCommerce product query and modifying the tax query to exclude products from a specific category on the shop page. By replacing 'category-slug-to-exclude' with the slug of the category you want to hide, you’ll ensure that no products from that category appear on the shop page.

Key Considerations

  • The excluded category products will still be accessible if customers know the product’s URL.
  • Only the shop page is affected; this won’t hide the products from category pages or search results.
  • Always ensure to back up your site before making changes to any code, and add this code to a child theme or custom plugin for best practices.

For more details on customizing product queries in WooCommerce, check out this WooCommerce Query Variables Guide.

Conclusion

With just a few lines of code, you can hide specific product categories from showing up on your WooCommerce shop page, giving you greater flexibility in how you display products. This method doesn’t affect any other parts of your site, ensuring a smooth user experience.

Scroll to Top

Request A Quote