Categories

Featured templates

WooCommerce. How to exclude subcategory products from category listing page

Rating: 4.0/5. From 4 votes.
Please wait...

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
This entry was posted in WooCommerce Tutorials and tagged category, listing, page, product, subcategory, WooCommerce. Bookmark the permalink.

Submit a ticket

If you are still unable to find a sufficient tutorial regarding your issue please use the following link to submit a request to our technical support team. We'll provide you with our help and assistance within next 24 hours: Submit a ticket