[contact-form-7 id="24078" title="Feedback EN"]

Featured templates

WooCommerce. How to exclude subcategory products from category listing page

This tutorial will show you how to exclude subcategory products from Woocommerce category listing page.

WooCommerce. How to exclude subcategory products from category listing page

This means that when viewing a main/parent category you will only view products of this single category and not the products of all containing subcategories.

This can be resolved by editing a core file of the child theme. Please follow the steps listed below :

  1. Navigate to Appearance > Editor and locate custom-functions.php of your child theme:

    how_to_exclude_subcategory_products_from_category_listing_page.1

  2. Open it to edit and paste the following code before the closing tag ?>

    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');
    

    how_to_exclude_subcategory_products_from_category_listing_page.2

  3. Save changes and visit your Shop page:

    how_to_exclude_subcategory_products_from_category_listing_page.3

  4. You can check if the number of products on the parent category listing page (“Grocery and Frozen”) corresponds to the actual number under Products > Categories:

    how_to_exclude_subcategory_products_from_category_listing_page.4

Feel free to check the detailed video tutorial below:

WooCommerce. How to exclude subcategory products from category listing page