Hide subcategories products in the macro-category in Woocommerce

I would like to show products and subcategories of a macro-category but I DON’T want to show the products of the subcategories.

Here’s an example of my problem: http://www.idromet.it/jml/wp/categoria-prodotto/prodotti/tubi-raccordi-acciaio-al-carbonio/

Read More

“Raccordi in ghisa zincati” is showed 2 times because the first is the category (and its right), the second one is the product of that sub-category (and I don’t want to show it in here).

Related posts

Leave a Reply

1 comment

  1. The code below should be pasted in the functions.php file located in your child theme folder.

    function exclude_product_cat_children($wp_query) {
    if ( isset ( $wp_query->query_vars['product_cat'] ) && $wp_query->is_main_query()) {
        $wp_query->set('tax_query', array( 
                                        array (
                                            'taxonomy' => 'product_cat',
                                            'field' => 'slug',
                                            'terms' => $wp_query->query_vars['product_cat'],
                                            'include_children' => false
                                        ) 
                                     )
        );
      }
    }  
    add_filter('pre_get_posts', 'exclude_product_cat_children');