WP Query – Get custom posts by terms across multiple taxonomies

So I have ids of custom terms, which belong to multiple taxonomies. A custom posts might have multiple terms, and also belong to multiple taxonomies. I want to query all the posts that match the terms ids, regardless of taxonomies. Currently I would do it like this:

//this is just an example. the idea is, that these terms are unsorted, and are from multiple taxonomies

$terms = array( 100, 102, 106, 108, 112);

$args = array(
‘post_type’ => ‘my_custom_post_type’,
‘tax_query’ => array(
‘relation’ => ‘AND’,
array(
‘taxonomy’ => ‘custom_tax_1’,
‘field’ => ‘term_id’,
‘terms’ => $terms
),
array(
‘taxonomy’ => ‘custom_tax_2’,
‘field’ => ‘term_id’,
‘terms’ => $terms
),
//repeat for all taxonomies, I would use a loop obviously
),
);
$query = new WP_Query( $args );

Is there a simpler/faster way to do it, using just the array of terms ids?

If not, regarding the example here, is it ok that I pass an unsorted array which might contain terms that don’t belong to a given taxonomy?

Read more here:: WP Query – Get custom posts by terms across multiple taxonomies

Leave a Reply

Your email address will not be published. Required fields are marked *