Show posts from two or more custom taxonomy terms

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

Related posts

Leave a Reply

2 comments

  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'
                )
            ),
        )
    );
    
  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'
                )
            )
        )
    );