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.

Categories

Related Blogs

Ajax Add to Cart Quantity on Shop WooCommerce

To enable AJAX add to cart with quantity selectors on the WooCommerce shop page, you’ll need to use JavaScript to handle the AJAX request and update the cart quantity dynamically. Below are the steps to achieve this:

Convert PHP Array into JavaScript Array.

To convert both a normal PHP array, a multidimensional PHP array, and a nested PHP array into JavaScript arrays, you can use JSON encoding and decoding as previously explained. JSON is well-suited for handling various data structures, including both simple arrays and complex nested arrays.

Upload Any File in My Account Registration Form

To allow users to upload a file in the My Account registration form in WooCommerce, you’ll need to customize the registration form and add a file upload field. We’ll use a custom function and a hook to achieve this. Here’s a step-by-step guide: