Ali Seivani
Ali Seivani

Reputation: 551

Wordpress: Multiple taxonomy terms in WP_Query

I am trying to get multiple taxonomy terms in WP_Query. Here is my code:

$category = get_field('portfolio_category');  //array of IDs like 14, 15, 16

            <?php
            $the_query = new WP_Query(array(
                'post_type'         => 'projects',
                'posts_per_page'    => -1,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'projectCategories',
                        'field' => 'term_id',
                        'terms' => array( implode(', ', $category ) ),
                        'operator' => 'AND'
                    )
                ),
            ));
            ?>

The problem is in the current code I can only query the very first term. For example I can only get projects from cat ID=14. What am I doing wrong here? How can I query posts from multiple terms?

Thank you.

Upvotes: 0

Views: 1277

Answers (1)

mikerojas
mikerojas

Reputation: 2338

Since $category is already an array... change your terms value to just $category. so your full query would be:

$the_query = new WP_Query(array(
    'post_type'         => 'projects',
    'posts_per_page'    => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'projectCategories',
            'field' => 'term_id',
            'terms' => $category,
            'operator' => 'AND'
        )
    ),
));

Upvotes: 1

Related Questions