I am trying to workout the best way to do a slightly abnormal orderby.
I have a query that returns 2 types (Post and Event). As expected by default it orders both, together, by publish date.
What I want is, posts to be ordered by publish date, events by event->start_date
I could just get them, loop over them and manually sort myself, but that seems counter intuitive.
Any help much appreceiated.
Cheers
Drogo
I am trying to workout the best way to do a slightly abnormal orderby.
I have a query that returns 2 types (Post and Event). As expected by default it orders both, together, by publish date.
What I want is, posts to be ordered by publish date, events by event->start_date
I could just get them, loop over them and manually sort myself, but that seems counter intuitive.
Any help much appreceiated.
Cheers
Drogo
Share Improve this question asked Mar 4, 2022 at 10:09 DrogoNevetsDrogoNevets 1012 bronze badges2 Answers
Reset to default 0You can do this by adding these two lines in your query:
'orderby' => 'publish_date',
'order' => 'ASC',
In the code below I assume that event->start_date
is a post meta field on the Event post type.
'orderby' => array(
'meta_value' => 'ASC',
'publish_date' => 'DESC'
),
'meta_key' => 'start_date',
'meta_type' => 'DATE',
This assumes that the Posts don't have the start_date
meta set, ie, that it's only present for the Events. Also I've assumed you want the Events in ascending order by their start_date and the Posts in descending publish_date order.
Reference
- WP_Query Order/Order By