4

I've been trying to hide a specific category from SHOP page. I found this code:

add_filter( '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;

    $q->set( 'tax_query', array(array(
        'taxonomy' => 'product_cat',
        'field' => 'slug',
        'terms' => array( 'CATEGORY TO HIDE' ),
        'operator' => 'NOT IN'
    )));

    remove_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

I've pasted this code in my theme function.php file but I'm not achieving the result...

Can anybody help me please?

1
  • 1
    Please fix the title of your post. All caps is just plain rude....
    – talonmies
    Nov 13, 2012 at 18:48

4 Answers 4

7

I know this is a bit late, but had this problem myself and solved it with the following function:

add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

function get_subcategory_terms( $terms, $taxonomies, $args ) {

  $new_terms = array();

  // if a product category and on the shop page
  if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {

    foreach ( $terms as $key => $term ) {

      if ( ! in_array( $term->slug, array( '**CATEGORY-HERE**' ) ) ) {
        $new_terms[] = $term;
      }

    }

    $terms = $new_terms;
  }

  return $terms;
}
4
  • 1
    I copy pasted the code. added category slug. removed is_shop(). but the category name is still being displayed on single product page of woocommerce.
    – Omar Tariq
    Nov 16, 2013 at 16:58
  • why did you remove is_shop() ?
    – danyo
    Nov 18, 2013 at 9:59
  • I just don't want it to appear anywhere on the website. Not only at shop pages. That's why.
    – Omar Tariq
    Nov 18, 2013 at 11:10
  • Did you ever get this figured out? I'm having the same issue and tried the above code with is_page() instead of is_shop() with still no luck.
    – Buster
    Nov 28, 2016 at 4:14
3

The following snippet it works fine for me:

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() && is_shop() ) {

        $q->set( 'tax_query', array(array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => array( 'your category slug' ), // Don't display products in the knives category on the shop page
            'operator' => 'NOT IN'
        )));

    }

    remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

I'm wondering how can I achieve the same for the products excluded within the category to be searchable via product search, while using the snippet those products gets totally hidden.

3

Simple way to hide a category from everything except the admin backend:

In functions.php:

add_filter( 'get_terms', 'hide_category', 10, 1 );
function hide_category( $terms ) {
  $new_terms = array();
  foreach ( $terms as $term ) {
    if ( $term->slug !== 'secret_category' ) {
      $new_terms[] = $term;
    } else if ( $term->taxonomy !== 'product_cat' || is_admin() ) {
      $new_terms[] = $term;
    }
  }
  return $new_terms;
}

If you want to only hide it from the shop, add || !is_shop() to the else if condition.

0

If you want to hide some categories in your theme you can just pass exclude argument in the wp_list_categories function:

wp_list_categories( array(
'taxonomy'              =>  'product_cat',
'hide_empty'            =>  1,
'use_desc_for_title'    =>  0,
'title_li'              =>  ' ',
'show_count'            =>  0,
'exclude'               =>  '63'    // <-- Hidden) );

Not the answer you're looking for? Browse other questions tagged or ask your own question.