admin管理员组文章数量:1386682
Any help on why this multiple meta query is not working is greatly appreciated, both meta_query
arguments have been tested independently.
I'm looking for bringing Featured products as well as Best Selling(popular) products. Currently this snippet of code is bringing all posts that are 'post_type' => 'product'
, completely ignoring the 'meta_query'
.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 10,
'meta_query' => array(
'relation' => 'OR',
array(
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num'
),
array(
'meta_key' => '_featured',
'meta_value' => 'yes'
)
)
);
Anybody out there in the wild?
Any help on why this multiple meta query is not working is greatly appreciated, both meta_query
arguments have been tested independently.
I'm looking for bringing Featured products as well as Best Selling(popular) products. Currently this snippet of code is bringing all posts that are 'post_type' => 'product'
, completely ignoring the 'meta_query'
.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 10,
'meta_query' => array(
'relation' => 'OR',
array(
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num'
),
array(
'meta_key' => '_featured',
'meta_value' => 'yes'
)
)
);
Anybody out there in the wild?
Share Improve this question edited Jan 14, 2016 at 21:51 ameraz asked Aug 17, 2015 at 11:19 amerazameraz 1454 silver badges9 bronze badges 2 |1 Answer
Reset to default 1meta_query
format and orderby
format should be as follows:
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page'=> 10,
'orderby' => 'total_sales',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_featured',
'value' => 'yes',
'compare' => '='
),
array(
'key' => 'total_sales',
'value' => '10',
'compare' => '>='
)
)
);
$query = new WP_Query( $args );
Instead of meta_key
and meta_value
use key
and value
with compare
as mentioned. You can see related documentation of WP_Meta_Query which also works with WP_Query.
本文标签: WooCommerceMultiple meta query not working
版权声明:本文标题:WooCommerce - Multiple meta query not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744550723a2612168.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'meta_key' => '_featured'
it shows Product 3, which is a featured product and if I query for'meta_key' => 'total_sales'
it shows Product 4, which is the best selling product. When I use the code above, it shows the last Product added, which is not featured neither best selling. Assuming that I'm querying for'posts_per_page' => 1
– ameraz Commented Aug 17, 2015 at 13:01