Exclude a Category From the Main Shop Page
Hides products from one or more categories on the main shop page while they remain available in their own category and by direct link.
Opens WPIDE on
Code
add_action( 'woocommerce_product_query', function ( $query ) {
$exclude = array( 'wholesale' ); // Category slugs to hide from the shop page.
if ( ! is_shop() ) {
return;
}
$tax_query = (array) $query->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $exclude,
'operator' => 'NOT IN',
);
$query->set( 'tax_query', $tax_query );
} );
Notes
List category slugs in $exclude.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.