If you’re running a WooCommerce store and want to hide products from specific categories, you might be wondering how to do it. Fortunately, WooCommerce provides a simple way to hide products from specific categories using a little bit of code.
Step 1: Determine the Category IDs to Hide
The first step in hiding WooCommerce products from specific categories is to determine the category IDs that you want to hide. You can do this by going to the WooCommerce Products page and hovering over the category name. In the URL that appears at the bottom of your browser, you should see a number following the text “term=”. This number is the category ID.
Step 2: Add Code to functions.php
Once you have the category IDs that you want to hide, you can add some code to your functions.php file. Here’s the code you’ll need to add:
function hide_products_from_category( $query ) {
if ( $query->is_main_query() && is_shop() ) {
$query->set( 'tax_query', array(
array (
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'category-slug-to-hide', 'another-category-slug-to-hide' ),
'operator' => 'NOT IN'
)
));
}
}
add_action( 'pre_get_posts', 'hide_products_from_category' );
Be sure to replace ‘category-slug-to-hide’ and ‘another-category-slug-to-hide’ with the actual slugs of the categories you want to hide.
Step 3: Save and Test
Save your functions.php file and test your WooCommerce store to make sure the products are hidden from the categories you specified.
By following these simple steps, you can easily hide WooCommerce products from specific categories using code. This can be a useful tool for store owners who want to provide a more streamlined shopping experience for their customers.