Skip to content

Instantly share code, notes, and snippets.

@woogists
Last active December 29, 2022 13:49
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save woogists/11ae5067dee442682ce2895f7b1645c2 to your computer and use it in GitHub Desktop.
Save woogists/11ae5067dee442682ce2895f7b1645c2 to your computer and use it in GitHub Desktop.
[Frontend Snippets] Exclude products from a particular category on the shop page
/**
* Exclude products from a particular category on the shop page
*/
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'clothing' ), // Don't display products in the clothing category on the shop page.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
@Moseby
Copy link

Moseby commented May 13, 2018

Is it possible to still make the products show when you specifically choose the cateogry from the category archive?

@sarahenry79
Copy link

Works great! Thank you!

@halilozanyilmaz
Copy link

Tags not working when this code is added to the product. Need a better solution.

@almiteyG
Copy link

I added this to functions.php but products under the clothing category still show on the shop page.

@johndcoy
Copy link

If you still want to hide products from the shop but still show products on the category archive page, use this snippet instead:

https://gist.github.com/neilgee/37a7392af2fb699178a9b9c1fca811b7#file-hide-prod-cat-woo-php

@imabidin
Copy link

Is there also an option to hide/exclude specific product categories from the whole frontend?

@elodie66000
Copy link

hello, I have a Parse error: syntax error, unexpected end of file that appears after adding the function in the functions.php file.thank you.

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