我使用多个分类查询来获取相关帖子
$tax_query[] = array(
'taxonomy' => 'transfer_type',
'field' => 'id',
'terms' => $page_terms['type_term'],
'include_children' => false
);
$tax_query[] = array(
'taxonomy' => 'area',
'field' => 'id',
'terms' => $page_terms['area_term'],
'include_children' => false
);
$args = array(
'post_type' => 'category_description',
'tax_query' => $tax_query
);
$description_post = get_posts($args);
当帖子被标记为transfer_type和区域时没有问题, 但是,当一篇文章仅用其中一个标记时,结果是错误的。
我基本上(在某些情况下)想要排除所有具有“区域”或“transfer_type”的帖子,而只获取那些与其他帖子相遇的帖子。
这可能吗?
最佳答案
弄清楚了......(不知道这是否是最好的,但它仍然是一个解决方案)
如果其中一个分类为空,我会对整个分类术语使用“NOT IN”运算符
$terms = get_terms("transfer_type");
foreach($terms as $term){
$not_in_type[] = $term->term_id;
}
$terms = get_terms("area");
foreach($terms as $term){
$not_in_area[] = $term->term_id;
}
$tax_query[] = array(
'taxonomy' => 'transfer_type',
'field' => 'id',
'terms' => $page_terms['type_term'] ? $page_terms['type_term'] : $not_in_type,
'include_children' => false,
'operator' => $page_terms['type_term'] ? 'IN' : 'NOT IN'
);
$tax_query[] = array(
'taxonomy' => 'area',
'field' => 'id',
'terms' => $page_terms['area_term'] ? $page_terms['area_term'] : $not_in_area,
'include_children' => false,
'operator' => $page_terms['area_term'] ? 'IN' : 'NOT IN'
);
关于wordpresstax_query,其中分类法为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17270615/