Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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'
            )
        )
    )
);
share|improve this question

2 Answers 2

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'
            )
        )
    )
);
share|improve this answer

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'
            )
        ),
    )
);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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