WP_Query orderby one custom field then another in one query

All we need is an easy explanation of the problem, so here it is.

I hope I can explain it properly so here we go!

I have a property listing page that should be ordered by the date a listing was posted (data comes form an API so it’s not the WP post date) and then any sold properties should be last in the list ordered by when they were sold with the most recently sold appearing first in that sub list. The first custom field is called listing_date and the other is agreed_date.

Does anyone know if this kind of ordering is possible? I was thinking I might need to use 2 queries but that would mean I wouldn’t be able to use pagination with that solution. Here’s my most recent failed attempt.

        <?php

       $args = array(
        'post_type' => 'listing',
        'posts_per_page' => 10,
        'paged' => $paged,
        'tax_query' => array(
            array(
                'taxonomy' => 'listingcategory',
                'field'    => 'slug',
                'terms'    => 'commercial',
            ),
        ),
        'orderby' => array(
            'agreed_date' => 'ASC',
            'listing_date' => 'DESC'
        ),      
       );
       $listing_posts = new WP_Query($args);

Thanks!

Updated code:

   $args = array(
    'post_type' => 'listing',
    'posts_per_page' => 10,
        'paged' => $paged,
        'tax_query' => array(
            array(
                'taxonomy' => 'listingcategory',
                'field'    => 'slug',
                'terms'    => 'commercial',
            ),
        'meta_query' => array(
            'relation' => 'AND',
            'listing_date_clause' => array(
                'key' => 'listing_date',
                'compare' => 'EXISTS',
            ),
            'agreed_date_clause' => array(
                'key' => 'agreed_date',
                'compare' => 'EXISTS',
            ), 
        ),
            ),
            'orderby' => array(
                'listing_date_clause' => 'DESC',
                'agreed_date_clause' => 'DESC'
            ),      
       );

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

According to https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters you want to specify it similar to the following code example:

$args = array(  
     'orderby' => array( 
        'title' => 'DESC', 
        'menu_order' => 'ASC' 
    ) 
); 
$query = new WP_Query( $args );

This is supported from WordPress 4.0.

For custom fields (post meta) you will need an added meta query, as detailed on https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query

E.g:

$q = new WP_Query( array(
'meta_query' => array(
    'relation' => 'AND',
    'state_clause' => array(
        'key' => 'state',
        'value' => 'Wisconsin',
    ),
    'city_clause' => array(
        'key' => 'city',
        'compare' => 'EXISTS',
    ), 
),
'orderby' => 'city_clause',
) );

This is supported since 4.2.

In your case (edited from your question) I think you would need:

$args = array(
    'post_type' => 'listing',
    'posts_per_page' => 10,
    'paged' => $paged,
    'tax_query' => array(
        array(
            'taxonomy' => 'listingcategory',
            'field'    => 'slug',
            'terms'    => 'commercial',
        )
    ),
    'meta_query' => array(
        'relation' => 'AND',
        'listing_date_clause' => array(
            'key' => 'listing_date',
            'compare' => 'EXISTS',
        ),
        'agreed_date_clause' => array(
            'key' => 'agreed_date',
            'compare' => 'EXISTS',
        ), 
    ),
    'orderby' => array(
        'listing_date_clause' => 'DESC',
        'agreed_date_clause' => 'DESC'
    ),      
);

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply