5

関連する投稿を取得するために複数の分類クエリを使用しています

    $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 でタグ付けされている場合は問題ありませんが、投稿がそれらの 1 つだけでタグ付けされている場合、結果は間違っています。

私は基本的に(場合によっては)「area」または「transfer_type」を持つすべての投稿を除外し、他のものと一致するものだけを取得したいと考えています。

出来ますか ?

4

1 に答える 1

3

それを理解しました...(それが最善かどうかはわかりませんが、それでも解決策です)

タクソノミーの 1 つが空の場合、タクソノミー用語全体で「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 に答える