Skip to content

Instantly share code, notes, and snippets.

@jameskoster
Forked from mikejolley/functions.php
Last active August 31, 2021 14:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jameskoster/4160591 to your computer and use it in GitHub Desktop.
Save jameskoster/4160591 to your computer and use it in GitHub Desktop.
WooCommerce - Exclude products from a particular category on the shop page
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() && is_shop() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'knives' ), // Don't display products in the knives category on the shop page
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
@Joyen12
Copy link

Joyen12 commented Jul 15, 2019

@interiorhacker Did you find an answer to your problem ?
I have the exact same problem and I've been looking at this for days...:(

I am desperate to find a solution so I write here hoping someone get a solution for this.

Thanks a lot

@kelvinramirez
Copy link

It works good, but I cant see the products of the excluded category in the back end in the list of all products, is there a way to fix it? thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment