5

Im using multiple taxonomy query to get relevant posts

    $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);

When a post is tagged with transfer_type and an area there is no problem, But when a post is only tagged with one of them the results are wrong.

I basically (in some cases) want to exclude all posts that have an "area" or "transfer_type" and get only those that meet up with the other.

is it possible ?

4

1 回答 1

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'
        );
于 2013-06-24T12:17:55.110 回答