Home › Forums › Front-end Issues › How to orderby multiple meta_key
I would like to sort my custom events by event date and then start time. Below is my current code:
$startDate = $_POST['startDate'];
$endDate = $_POST['endDate'];
$eventType = $_POST['eventType'];
$args = array(
'post_type' => 'event',
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => array( $startDate , $endDate ),
'compare' => 'BETWEEN'
),
array(
'key' => 'event_type',
'value' => $eventType,
'compare' => 'LIKE',
)
)
);
$the_query = new WP_Query( $args );
How do I also order by start time?
Thanks!
Matt
WP 4.0 introduced a better order by https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/ and this is detailed in the codex http://codex.wordpress.org/Class_Reference/WP_Query
WP 4.2 added the clause for meta_query https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/ This link covers just about everything you need.
That worked thanks! Just added
'orderby' => array(
'event_date' => 'ASC',
'time_start' => 'ASC'
),
Matt
hi mdfarver, I trying to do something similar but it’s not working for me.
Can you paste the entire args array code?
What WP version are you using? This only works on 4.2? I’m on 4.1.8…
Thank’s in advance!
I’m also having an issue with this using 4.4. It definitely worked with 4.2, not sure if something change in 4.4 or maybe 4.3
Thank’s for your reply.
After update to 4.2 seems it’s working.
The topic ‘How to orderby multiple meta_key’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.