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

wp query - WP_Query order by multiple meta keys & fields

programmeradmin5浏览0评论

How to order query by multiple meta keys and fields/custom fields?

This is my code:

$args = array
    (
        'post_type' => 'listing',
        'posts_per_page' => -1,
        'tax_query' => array
        (
            array
            (
                'taxonomy' => 'listing_category',
                'field' => 'slug',
                'terms' => urldecode($category)
            ),
            array
            (
                'taxonomy' => 'location',
                'field' => 'slug',
                'terms' => urldecode($location)
            )
        ),

        //*** THIS NOT WORK
        'meta_query'    => array
        (
            array(
                'key'     => 'listing_status',
                'orderby' => 'meta_value',
                'order' => ASC,
            ),
            array(
                'key'     => 'listing_total_rank',
                'orderby' => 'meta_value',
                'order' => DESC,
            ),
            array(
                'key'     => 'listing_free_date',
                'orderby' => 'meta_value',
                'order' => ASC,
            ),
            array(
                'key'     => 'title',
                'orderby' => 'meta_value',
                'order' => ASC,
            ),
        ),
        //***
    );

$listings = new WP_Query( $args );

The part of 'meta_query' does not working in the code.

How to order query by multiple meta keys and fields/custom fields?

This is my code:

$args = array
    (
        'post_type' => 'listing',
        'posts_per_page' => -1,
        'tax_query' => array
        (
            array
            (
                'taxonomy' => 'listing_category',
                'field' => 'slug',
                'terms' => urldecode($category)
            ),
            array
            (
                'taxonomy' => 'location',
                'field' => 'slug',
                'terms' => urldecode($location)
            )
        ),

        //*** THIS NOT WORK
        'meta_query'    => array
        (
            array(
                'key'     => 'listing_status',
                'orderby' => 'meta_value',
                'order' => ASC,
            ),
            array(
                'key'     => 'listing_total_rank',
                'orderby' => 'meta_value',
                'order' => DESC,
            ),
            array(
                'key'     => 'listing_free_date',
                'orderby' => 'meta_value',
                'order' => ASC,
            ),
            array(
                'key'     => 'title',
                'orderby' => 'meta_value',
                'order' => ASC,
            ),
        ),
        //***
    );

$listings = new WP_Query( $args );

The part of 'meta_query' does not working in the code.

Share Improve this question edited Aug 28, 2017 at 14:04 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Aug 28, 2017 at 13:38 SeviSevi 3152 gold badges4 silver badges8 bronze badges 1
  • 1 I would note that this query is going to be quite slow/expensive – Tom J Nowell Commented Aug 28, 2017 at 14:56
Add a comment  | 

1 Answer 1

Reset to default 19

You are using meta query without setting a value. The way you are doing it is using to query posts, not to order them.

Using Named Meta Queries

To order your posts by different meta datas, you can give your meta queries a name and then use that to set the ordering. Here is a simple example for you:

$args = array(
    'meta_query' => array(
        'relation' => 'AND',
        'query_one' => array(
            'key' => 'key_one',
            'value' => 'value_one', // Optional
        ),
        'query_two' => array(
            'key' => 'key_two',
            'compare' => 'EXISTS', // Optional
        ), 
    ),
    'orderby' => array( 
        'query_one' => 'ASC',
        'query_two' => 'DESC',
    ),
);

You can check this section of codex page for WP_Query() to familiarize yourself with sorting the posts.

发布评论

评论列表(0)

  1. 暂无评论