最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Wrong Number of Products When Hiding Subcategory Products From Showing In Parent Category

programmeradmin2浏览0评论

I'm using following code to display only parent category products and remove the child category products displayed in the catalog:

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

If you now select the parent category in the catalog, only the products from this category are displayed.

The code works as it should except for one small flaw:
The number of products per catalog page is no longer correct and varies from page to page depending on how many products of the subcategory were hidden.

I could not find any further information about this problem although this code seems to be used more often.

发布评论

评论列表(0)

  1. 暂无评论