Skip to content

Instantly share code, notes, and snippets.

@omniacode
Created October 22, 2020 15:13
Show Gist options
  • Save omniacode/f95f9459db43ba51a9359d1d09af205c to your computer and use it in GitHub Desktop.
Save omniacode/f95f9459db43ba51a9359d1d09af205c to your computer and use it in GitHub Desktop.
Exclude Specific Product Categories from WooCommerce Shop Page
// Exclude products from a particular category on the shop page
function ocs_custom_pre_get_posts_query( $q ) {
if( is_shop() || is_page('shop') ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'cat-1', 'cat-2', 'cat-3' ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
}
add_action( 'woocommerce_product_query', 'ocs_custom_pre_get_posts_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment