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 badges1 Answer
Reset to default 3The 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