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
相关产品推荐
相关产品推荐

