admin管理员组文章数量:1425946
How can I select all products that have a custom field 'is_new' set to 'Yes'?
This is what I tried:
$loop = new WP_Query([
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => [
'taxonomy' => 'is_new',
'terms' => 'YES',
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
]
]);
Unfortunately, this just gives me all products.
How can I select all products that have a custom field 'is_new' set to 'Yes'?
This is what I tried:
$loop = new WP_Query([
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => [
'taxonomy' => 'is_new',
'terms' => 'YES',
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
]
]);
Unfortunately, this just gives me all products.
Share Improve this question edited May 30, 2019 at 21:08 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked May 30, 2019 at 20:12 AndrewAndrew 11 Answer
Reset to default 0You're attempting to do a "taxonomy query". This is for things like tags and categories. Custom fields are stored as meta, so you need to do a meta query.
$loop = new WP_Query( [
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => [
[
'key' => 'is_new',
'value' => 'Yes',
],
],
] );
There's a few other differences to take note of:
- From your screenshot, the value for yes is
Yes
, notYES
. - Taxonomy and meta queries need to be an array of arrays. See how my meta query is an array inside another array.
- Since it's not a taxonomy query, we use
compare
instead ofoperator
, but the default value for that is=
, which we want, so we can omit it.
本文标签: woocommerce offtopicSelect all products that have a custom field 39isnew39 set to 39Yes39
版权声明:本文标题:woocommerce offtopic - Select all products that have a custom field 'is_new' set to 'Yes' 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745449265a2658807.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论