Here’s how to exclude one or more category in your woocommerce shop page. This way you can hide products that are packages or subscription which belong to a page or has its own page. This is tested in Woocommerce 2.0 and WordPress 3.6.

Add this in your current theme functions.php
[php]

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() ) {

$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'PUT YOUR CATEGORY HERE' ), // Don't display products in the membership category on the shop page . For multiple category , separate it with comma.
'operator' => 'NOT IN'
)));

}

remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

[/php]

This will work in older version :
Exclude product category in older version of woocommerce.



20 thoughts on “How to exclude one or more category in woocommerce shop page

  1. I keep seeing this code for excluding a category, but not one person says where to put it!
    I have never edited the woocommerce php files before so could you give me a bit more direction?
    thanks!

    • Arni Cinco

      Hi Julia ,

      It should be pasted in your current theme functions.php . I already updated the post. Thank you

  2. I’ve tried this but it didn’t work. I’m using woocommerce and woo artificer theme for my shop. Trying so many codes to exclude the category from my shop page but no result. Could you give me some advice?

  3. Tracey

    Thanks for this! Worked perfectly.

    Actually it works in reverse as well, just in case anyone wants to display only certain categories, rather than exclude, just change

    ‘operator’ => ‘NOT IN’

    to

    ‘operator’ => ‘IN’

    • Arni Cinco

      You are welcome! Thanks for the contribution.

  4. I followed the instructions exactly and it doesn’t work for us either! It can’t really be this hard.

    • Arni Cinco

      What is your woocommerce version? This has been tested in woocommerce 2.0.

  5. Ferg

    Hi. Is it possible to exclude a category depending on what day of the week and what time it is? I’m doing an online order site for a restaurant. They have a different menu for each day and orders have to be placed before 11am on the day of collection. I’ve made a category for each days menu.

    Thanks

    • Arni Cinco

      Hi Ferg,

      You need to get the day name today and exclude it from other days. Category names should be , “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”. Example
      [php]

      $arr_days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
      $remove_day=date("l");//get the day today
      $new_arr = array_diff($arr_days, array($remove_day)); // remove the day today

      $q->set( ‘tax_query’, array(array(
      ‘taxonomy’ => ‘product_cat’,
      ‘field’ => ‘slug’,
      ‘terms’ => $new_arr, // Don’t display products in the membership category on the shop page . For multiple category , separate it with comma.
      ‘operator’ => ‘NOT IN’
      )));
      [/php]

      Good luck!

  6. HI, thanks for this snippet!
    Is there anyway to remove ALL of the titles of the categories on the shop page?
    So instead of adding them all to your snippet above, have some sort of a ALL function?

    Thanks!

    • Arni Cinco

      Hi Hank ,

      Sorry, im not sure i understand.

  7. I’ve tried this script and 4 others and nothing is working. I’m on Woo v2.2.4 and WP 4.0. I’m placing it underneath my other custom functions in the functions.php file of my theme. Any thoughts?

    This is my code:
    [php]
    add_action( ‘pre_get_posts’, ‘custom_pre_get_posts_query’ );
    function custom_pre_get_posts_query( $q ) {

    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;
    if ( ! is_admin() ) {

    $q->set( ‘tax_query’, array(array(
    ‘taxonomy’ => ‘product_cat’,
    ‘field’ => ‘slug’,
    ‘terms’ => array( ‘gift-box-black-velour’, ‘gift-box-premium-leatherette’ ), // Don’t display products in the membership category on the shop page . For multiple category , separate it with comma.
    ‘operator’ => ‘NOT IN’
    )));
    }
    remove_action( ‘pre_get_posts’, ‘custom_pre_get_posts_query’ );
    }
    [/php]

  8. Hira Singh

    Hi,

    You did a good job. I have same problem in my code. I have to exclude some products from loop who have specific meta value. Like I have a product with meta key ‘_abc’ and meta value ‘true’, that should not be displayed in loop.

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

clear formSubmit