0

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

2 Answers 2

0

You can do this by adding these two lines in your query:

'orderby' => 'publish_date',
'order' => 'ASC',
1
  • this is fine for the posts, but the events i want to use the start_date not the publish_date Mar 4, 2022 at 11:23
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.