wordpress tax_query,其中分类法是空白的

lio*_*r r 5 wordpress taxonomy

我使用多个分类查询来获取相关帖子

    $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);
Run Code Online (Sandbox Code Playgroud)

当一个帖子用transfer_type和一个区域标记时没有问题,但是当一个帖子只用其中一个标记时,结果是错误的.

我基本上(在某些情况下)想要排除所有具有"area"或"transfer_type"的帖子,并且只获得与其他帖子相遇的帖子.

可能吗 ?

lio*_*r r 4

想通了...(不知道这是否是最好的,但它仍然是一个解决方案)

如果其中一个分类为空,我将在整个分类术语上使用“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'
        );
Run Code Online (Sandbox Code Playgroud)