Remove child products from woocommerce category page [closed]

you want to display only parent category products and remove the child category products. Add the following lines of code at the end of your theme’s functions.php file.

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

Leave a Comment