- Web templates
- E-commerce Templates
- CMS & Blog Templates
- Facebook Templates
- Website Builders
WooCommerce. How to exclude subcategory products from category listing page
January 13, 2016
This tutorial will show you how to exclude subcategory products from Woocommerce category listing page.
WooCommerce. How to exclude subcategory products from category listing pageThis 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 :
Navigate to Appearance > Editor and locate custom-functions.php of your child theme:
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');
- Save changes and visit your Shop page:
- 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:
Feel free to check the detailed video tutorial below:
WooCommerce. How to exclude subcategory products from category listing page