Skip to main content
Tweeted twitter.com/StackWordPress/status/732199921005268992
added 754 characters in body
Source Link

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'
            ),      
       );

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'
            ),      
       );
Source Link

WP_Query orderby one custom field then another in one query

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!