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.
'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