我使用多个分类查询来获取相关帖子
$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和区域标记帖子时没有问题, 但是当帖子只标记其中一个时,结果是错误的。
我基本上(在某些情况下)想要排除所有具有“area”或“transfer_type”的帖子,并且只获得那些与另一个相遇的帖子。
有可能吗?
答案 0 :(得分:3)
想出来......(不知道它是否是最好的,但它仍然是一个解决方案)
如果其中一个分类法为空,我在整个分类术语中使用“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'
);