Skip to content

Instantly share code, notes, and snippets.

@woogists
Last active December 29, 2022 13:49
Show Gist options
  • Select an option

  • Save woogists/11ae5067dee442682ce2895f7b1645c2 to your computer and use it in GitHub Desktop.

Select an option

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

Moseby commented May 13, 2018

Copy link
Copy Markdown

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

@sarahenry79

Copy link
Copy Markdown

Works great! Thank you!

@halilozanyilmaz

Copy link
Copy Markdown

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

@almiteyG

Copy link
Copy Markdown

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

@johndcoy

Copy link
Copy Markdown

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
Copy Markdown

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

@elodie66000

Copy link
Copy Markdown

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