Hide WooCommerce Product Categories on Shop Page or other Page

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…

<?php //<~ dont add me into functions.php
add_action( 'woocommerce_product_query', 'prefix_custom_pre_get_posts_query' );
/**
* Hide Product Cateories from targetted pages in WooCommerce
* @link https://gist.github.com/stuartduff/bd149e81d80291a16d4d3968e68eb9f8#file-wc-exclude-product-category-from-shop-page-php
*
*/
function prefix_custom_pre_get_posts_query( $q ) {
if( is_shop() || is_page('awards') ) { // set conditions here
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'cat1', 'cat2' ), // set product categories here
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
}

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.

ref

9 Comments

  1. Mark Simkiss on May 24, 2019 at 4:08 pm

    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)

  2. +ES on April 15, 2019 at 7:40 pm

    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!

  3. Jonathan on June 13, 2018 at 4:38 am

    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?

  4. Luc on April 20, 2018 at 2:22 pm

    How instead of page name “award” can I use the page id?

    • Neil Gowran on April 20, 2018 at 8:55 pm

      Put id number in the parentheses

  5. David Dunnington on April 19, 2018 at 11:50 am

    Was looking for this for quite a while. Worked perfectly thank you.

  6. siyad on February 9, 2018 at 12:57 pm

    its working thanx

  7. Micki on November 23, 2017 at 1:15 am

    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!

Leave a Comment