Remove product categories from shop page

If you want to get rid of a certain product category from your shop page, this code is very useful. The code will hide all the products from the mentioned categories.

add_action( ‘pre_get_posts’, ‘remove_categories_shop’ );
function remove_categories_shop( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() && is_shop() && ! is_user_logged_in() ) {
$q->set( ‘tax_query’, array(array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
// Don’t display products in these categories on the shop page
 ‘terms’ => array( ‘color’, ‘flavor’, ‘spices’, ‘vanilla’ ),
‘operator’ => ‘NOT IN’
)));
 }
remove_action( ‘pre_get_posts’, ‘remove_categories_shop’ );
}

Leave a Reply

Your email address will not be published. Required fields are marked *