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

Jquery Form Validator With User Name Exist User Email Exist

To implement jQuery Form Validator with the specified validation rules, including required, minlength, email, equalTo, custom validation, confirm password, username pattern, email existence in the database in WordPress, and username with no numeric value, you’ll need to combine client-side validation using jQuery Form Validator with server-side validation using PHP and WordPress functions.

Shortcodes included with Dokan

Dokan is a popular multi-vendor marketplace plugin for WooCommerce that allows you to create and manage a marketplace where multiple vendors can sell their products. Dokan provides several shortcodes that you can use to display various elements and functionalities on your marketplace pages. Here are some of the essential shortcodes provided by Dokan:

How to know “Who created this coupon?”

What to do when your client ask “Who created this coupon?” . in WooCommerce.  But we don’t have any default things in WooCommerce  which automatically show author of coupon .