1

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?

2
  • How is it not working? What do you get, as opposed to what do you expect? Commented Aug 17, 2015 at 11:57
  • Its bringing all products. For instance, in my development environment, if I query for '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

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.