最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WooCommerce - Multiple meta query not working

programmeradmin3浏览0评论

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
  • How is it not working? What do you get, as opposed to what do you expect? – TheDeadMedic 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
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论