How to hide particular category product on shop

To hide a particular category on the shop page using the `woocommerce_product_query` hook, you can modify the query parameters to exclude the category you want to hide. This approach allows you to customize the product query directly without modifying the main query.

Here’s how you can achieve this:

 

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' );

 

 

Make sure to replace `’category-slug-to-hide’` with the actual slug of the category you want to exclude from the shop page.

With this code, the products in the specified category will not be displayed on the shop page, but they will still be accessible through their direct URLs and will appear in other relevant product listings or searches.

Remember to add this code to your theme’s functions.php file or a custom plugin. Always take a backup of your site before making any changes to the code.

Leave a Comment

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