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.

Related Blogs

How to Add Google reCAPTCHA

To use Google reCAPTCHA in your website, you need to obtain a reCAPTCHA API key and secret. Follow these steps to get your reCAPTCHA credentials:

How to add ACF options pages and options sub page

To add Advanced Custom Fields (ACF) options pages and options sub-pages, you’ll need to have ACF Pro installed and activated on your WordPress site. ACF Pro allows you to create custom options pages where you can add settings, fields, and other configuration data.

Request A Quote

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.