WP Query - Get custom posts by terms across multiple taxonomies

by Maciej Krawczyk   Last Updated March 11, 2018 20:08 PM

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?



Related Questions



Get terms from current WP Query

Updated May 07, 2017 01:08 AM

Output taxonomy terms if uses another taxonomy

Updated September 10, 2017 18:08 PM


Filtering WP_Query Dynamically on the Front-End

Updated November 07, 2017 07:08 AM