1

I want to show posts which are filed 2 or more custom taxonomy terms. For example, I want to show the posts from custom post type "Classified" and filed under Books and House terms.

The code below show posts which are marked in either of the terms. I want only the posts marked in both the terms:

$args = array(
    'posts_per_page' => 10,  // Number of posts per page
    'post_type' => 'classifieds',   // Custom Post Type like Movies
    'tax_query' => array(
        array(
            'taxonomy' => 'classifieds_tags',   //Custom Taxonomy Name like Genre
            'field' => 'slug',
            'terms' => array(
                'books',    //Tags or Categories like Drama or Comedy
                'houses'
            )
        )
    )
);

2 Answers 2

1

You need to combine two tax queries with and AND relation:

$args = array(
    'posts_per_page' => 10,  // Number of posts per page
    'post_type' => 'classifieds',   // Custom Post Type like Movies
    'tax_query' => array(
        'relation' => 'AND'
        array(
            'taxonomy' => 'classifieds_tags',   //Custom Taxonomy Name like Genre
            'field' => 'slug',
            'terms' => array(
                'books' //Tags or Categories like Drama or Comedy
            )
        ),
        array(
            'taxonomy' => 'classifieds_tags',   //Custom Taxonomy Name like Genre
            'field' => 'slug',
            'terms' => array(
                'houses'
            )
        ),
    )
);
0

I think you need to add the relationargument :

$args = array(
    'posts_per_page' => 10,  // Number of posts per page
    'post_type' => 'classifieds',   // Custom Post Type like Movies
    'tax_query' => array(
        array(
            'taxonomy' => 'classifieds_tags',   //Custom Taxonomy Name like Genre
            'field' => 'slug',
            'relation' => 'AND',
            'terms' => array(
                'books',    //Tags or Categories like Drama or Comedy
                'houses'
            )
        )
    )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.