Multiple custom fields for ‘orderby’ in ‘WP_Query’

meta_query and orderby are seperate parameters, you just put them together in an array. You will have to do one then the other.

e.g.

<?php
$args = array(
    'post_type'     => 'property',
    'meta_query'    => array(
        array(
            'relation' => 'AND',
            'city_clause' => array(
                'key'       => 'city',
                'compare'   => 'EXISTS',
            ),
            'street_clause' => array(
                'key'       => 'street_name',
                'compare'   => 'EXISTS',
            ),
        )
    )
    'orderby' => array(
        'city_clause'       => 'desc',
        'street_clause'     => 'desc',
    )
)
?>

https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

Leave a Comment