wp query - Pass array of taxonomy terms to wp_query

admin2025-06-06  8

I am trying to compile an array of taxonomies and terms to pass to wp_query, my array looks like this...

Array
(
    [0] => Array
        (
            [taxonomy] => color
            [field] => term_id
            [terms] => Array
                (
                    [0] => 15
                )

        )

    [1] => Array
        (
            [taxonomy] => shape
            [field] => term_id
            [terms] => Array
                (
                    [0] => 87
                )

        )

    [2] => Array
        (
            [taxonomy] => weight
            [field] => term_id
            [terms] => Array
                (
                    [0] => 3
                    [1] => 54
                )

        )

    [3] => Array
        (
            [taxonomy] => rating
            [field] => term_id
            [terms] => Array
                (
                    [0] => 87
                    [1] => 88
                )

        )

)

I am trying to pass it to wp_query like this...

$args = array(
    'post_type' => 'post',
    'relation' => 'OR',
    'tax_query' => $myarray,
);
$query = new WP_Query( $args );

This is not giving me any results, where am I going wrong?

I am trying to compile an array of taxonomies and terms to pass to wp_query, my array looks like this...

Array
(
    [0] => Array
        (
            [taxonomy] => color
            [field] => term_id
            [terms] => Array
                (
                    [0] => 15
                )

        )

    [1] => Array
        (
            [taxonomy] => shape
            [field] => term_id
            [terms] => Array
                (
                    [0] => 87
                )

        )

    [2] => Array
        (
            [taxonomy] => weight
            [field] => term_id
            [terms] => Array
                (
                    [0] => 3
                    [1] => 54
                )

        )

    [3] => Array
        (
            [taxonomy] => rating
            [field] => term_id
            [terms] => Array
                (
                    [0] => 87
                    [1] => 88
                )

        )

)

I am trying to pass it to wp_query like this...

$args = array(
    'post_type' => 'post',
    'relation' => 'OR',
    'tax_query' => $myarray,
);
$query = new WP_Query( $args );

This is not giving me any results, where am I going wrong?

Share Improve this question asked Nov 12, 2018 at 15:03 fightstarr20fightstarr20 1,1358 gold badges26 silver badges47 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

The problem is ambiguity, you can't just put in 'relation' => 'OR' in the main parameter list, because how would WP_Query know if it's for the tax_query and not the meta_query?

For how it's supposed to work, and where the relation parameter goes we need to refer to the official docs for WP_Query which gives us this example:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'quotes' ),
        ),
        array(
            'taxonomy' => 'post_format',
            'field'    => 'slug',
            'terms'    => array( 'post-format-quote' ),
        ),
    ),
);
$query = new WP_Query( $args );

Setting it may be as simple as:

$myarray['relation'] = 'OR'; // untested
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749189251a317110.html

最新回复(0)