How to get posts which must match both custom taxonomies?

I have searched in the stackexchange site for an answer for this question. However, I did not find any posts can help me solve the following problem:

I have 2 custom taxonomies named tax1 and tax2

Read More

I want to create a args for WP_Query which must meet the following conditions:
– The posts must be in both tax2 and tax2
– The posts are in the custom post_type posttype1

I have tried to create the following args to get the posts but it did not work as I expected:

$query_args = array(
        'post_type' => 'posttype1',
        'ignore_sticky_posts' => 1,
        'posts_per_page' => 10,
        'offset' => 0,
        'orderby' => date,
        'order' => ASC,
        'post__not_in' => array( get_the_ID() ),
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'tax1',
                'field' => 'id',
                'terms' => array ( ... ), // an array of term
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => 'tax2',
                'field' => 'id',
                'terms' => array ( ... ), // an array of term
                'operator' => 'IN'
            )
        )
    );

Could anyone help me to solve this problem please?

Thanks

Related posts

Leave a Reply