Alex Rivera | Logout

How to order by multiple meta keys?

Asked 2013-07-19T11:45:10.740
28

I’m using a custom loop to display my events on a page, I get it fine ordering by the event start date using the below:

$args = array( 
    'post_type' => 'event',
    'order' => 'ASC',
    'orderby' => 'meta_value',
    'meta_key' => '_event_start_date');
$loop = new WP_Query( $args );

But the meta_key option only allows one value. How to use two values (_event_start_date and _event_start_time)?

Edit
Report

1 Answer

-2

From WP_Query documentation, use meta_query and fill up with an array of meta key/values:

$args = array(
    'post_type' => 'product',
    'meta_query' => array(
        array(
            'key' => 'color',
            'value' => 'blue',
            'compare' => 'NOT LIKE'
        ),
        array(
            'key' => 'price',
            'value' => array( 20, 100 ),
            'type' => 'numeric',
            'compare' => 'BETWEEN'
        )
    )
);
$query = new WP_Query( $args );
answered 2013-07-19T12:24:27.463

Your Answer