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

WP 6.1.1查询自定义分类术语相关帖子却返回其他分类结果

解决WordPress自定义分类法查询过滤失效问题

你的查询返回不符合条件的帖子,核心原因是tax_query的结构错误。WP_Query的tax_query参数要求传入一个包含查询条件数组的外层数组,哪怕只有一个分类条件也不能省略这个外层数组。

修正后的查询代码

// related places query
$posts_rel_args = array(
    'post__not_in' => array($place_id), // 排除当前文章
    'post_type' => 'place',
    'posts_per_page' => 3,
    'post_status' => 'publish', // 明确指定获取已发布文章,可选但推荐
    'tax_query' => array(
        array( // 外层数组包裹分类条件
            'taxonomy' => 'country',
            'field' => 'term_id',
            'terms' => array($cur_place_related_term_id),
            'operator' => 'IN' // 明确匹配方式,默认即为IN,可选
        )
    )
);
$posts_rel = new WP_Query($posts_rel_args);

额外排查点

  • 确认$cur_place_related_term_id确实是德国分类的正确term_id,可通过get_term_by('slug', 'germany', 'country')获取并对比
  • 确认自定义分类法country在注册时已将place加入object_type参数,确保两者关联正确

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 03:52:11