You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wordpress自定义分类法下如何仅获取当前分类文章排除子分类

解决方案

在tax_query配置项中新增include_children参数,设置为false即可。WordPress分类查询默认开启包含子分类的规则,关闭该参数后就只会精准匹配指定ID的分类下的文章。

修改后的完整代码如下:

$cat = get_queried_object(); // Current category

$args = array(
        'posts_per_page' => -1,
        'post_type' => 'products',
        'orderby' => 'title',
        'order'   => 'ASC',
        
        'tax_query' => array(
          array(
            'taxonomy' => 'section',
            'field' => 'term_id',
            'terms' => $cat->term_id,
            'include_children' => false, // 关闭子分类内容匹配
          )
        )
                 
    );

    $products = get_posts($args);

参数说明

  • include_children是WordPress tax_query的原生参数,默认值为true,专门用于控制分类查询是否自动拉取对应分类的所有层级子分类内容。

内容的提问来源于stack exchange,提问作者www

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 01:24:04