Last active
December 29, 2022 13:49
-
-
Save woogists/11ae5067dee442682ce2895f7b1645c2 to your computer and use it in GitHub Desktop.
[Frontend Snippets] Exclude products from a particular category on the shop page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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' ); |
Tags not working when this code is added to the product. Need a better solution.
I added this to functions.php but products under the clothing category still show on the shop page.
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
Is there also an option to hide/exclude specific product categories from the whole frontend?
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
Works great! Thank you!