admin管理员组文章数量:1130349
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
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
本文标签: wp queryPass array of taxonomy terms to wpquery
版权声明:本文标题:wp query - Pass array of taxonomy terms to wp_query 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749189227a2329905.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论