Background: I have a custom post type "events" which has a post meta value of a unix timestamp called event start date. I also have a post meta value of ticket_status that has 3 possible values (S=Sold Out, Y=On Sale, I=Invite Only). I currently have it sorting by the event start date from earliest to latest.
Issue: I want the events to display from earliest to latest as I have them now, but I want all sold out events to appear at the bottom of the list.
I currently have this as my $args as:
'post_type' => array('events'),
'posts_per_page' => 25,
'post_status' => 'publish',
'meta_key' => "event_start_date",
'orderby' => "meta_value title',
'order' => 'ASC'
I also have a filter which is returning $orderby:
$orderby .= ", wp_postmeta.meta_value " . $query->query_vars['order'];
I have tried ordering by event_start_date then by ticket_status, but then it orders them by 1)On sale events, 2)invite events and then 3)sold out events. This results in the events showing in the wrong chronological order.
Is it possible to do a case statement? Similar to this:
ORDER BY CASE WHEN ticket_status = 'S' THEN 1 ELSE 0 END, event_start_date ASC
Anything that can help me do this would be awesome! Thank you in advance.
wp_rewind_posts()
. Run your query once, looping through to display Y & I values, sorted by date. Rewind and loop again on the same query result, displaying S values at the bottom. codex.wordpress.org/Function_Reference/rewind_posts