You can hide certain WooCommerce product categories from the shop page or other targetted pages, by using an action called woocommerce_product_query
Add it in your theme via functions.php and it works like so…
So the action is wrapped in a conditional statement that targets the pages you want the Product Categories to not display.
Then in the $tax_query array set the terms value array to the actual Product Categories you need to be removed.
18 comments
Vasil
Hello and thanks for the code, the code works perfectly on my website but is there a way to still show the hidden products from the hidden category in the search results?
here is my code:
function prefix_custom_pre_get_posts_query( $q ) {
if( is_shop() || is_page(‘shop’)) { // set conditions here
$tax_query = (array) $q->get( ‘tax_query’ );
$tax_query[] = array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
‘terms’ => array( ‘дизайнерски-мебели-kare’, ), // set product categories here
‘operator’ => ‘NOT IN’
);
$q->set( ‘tax_query’, $tax_query );
}
}
Marcel Kraan
And YES you are the best!!!!
Thank you this works as a Rolls Royce
Now i try to make my own plugin with options in Woocommerce settings!
Marcel Kraan
I have changed a condition and it seems that it works
I only changed this line
// if( is_shop() || is_page(‘awards’) ) { // set conditions here
if ( is_page(‘Home’) ) { // set conditions here
guy man dude
Finally – the right solution. Thanks.
Titus
Thank you, saved me quite a bit of time.
WPseason
How Can I hide product in Woocommerce but still available when user has the URL of the page?
I just want to hide it from the list in shop and category page.
Thanks
Neil Gowran
Set the catalog visibility option to hidden – ref https://docs.woocommerce.com/document/managing-products/#section-10
aaron davis
This helped me so much, easy to setup and do for people with only a little bit of coding knowledge.
nic
does not work!
Mark Simkiss
There is hope! Trying to figure out something that seems easy but I don’t have the expertise. Perhaps it would be easy for you? What I need to do is exclude products in specific categories (or with specific tags) from appearing on the main shop page. I want them show up on the category URL: http://www.website.comm/store/clientname as they are all categorized with “clientname”. Works great. However, they also show up on the main shop page “www.website.comm” from which I would like to hide them. I only want them to show up if you go to the category URL. How would that be done? (also, all without a password/login)
Mark Simkiss
…almost as if you could have conceptually…
if( is_shop() || is_page(‘www.website.comm’) ) {and not http://www.website.comm/store/clientname}
+ES
Hello+
Thank you very much for posting this information and code! I am very interested in using this, but I have about 50 categories for my bakery shop and I only want to show 1 category on the Shop Page. Is there a way to say “include this 1 category only” instead of “exclude these 49 categories” via code…
Right now I would have to add all 49 categories and as I make new categories (for new baked goods) I would need to add each of those to the code-list. But, if the code said show one category that would be an easier way for me as I will always want to show only that one category on the Shop page (no matter how many new ones I create).
I can provide additional information, if you need it. Thank you for your guidance and time on this!
Jonathan
Hello. This is great and I really appreciate you sharing.
One question: I’d like to exclude categories on the ‘products’ page and any subpage of ‘products’, for example ‘products/page/2/’
Is there a way in the code to target the main page and all child pages?
Luc
How instead of page name “award” can I use the page id?
Neil Gowran
Put id number in the parentheses
David Dunnington
Was looking for this for quite a while. Worked perfectly thank you.
siyad
its working thanx
Micki
Hi, this is not working, though I was excited to find it!
I replaced:
if( is_shop() || is_page(‘awards’) ) with is_page(‘my-shop-page’) )
And replaced/added my category slugs here:
‘terms’ => array( ‘cat1’, ‘cat2’ )
will it only work with new categories created after adding the snippet? thanks!