In the Bricks Facebook group a user asks:
Hello everyone. I have a CPT ‘events’ and a taxonomy ‘event-did-it-happen’ in which there are two items ‘past’ and ‘upcoming’.
I need to list all the ‘events’ posts sorted by ‘event-did-it-happen’ in a loop so that they are ‘upcomming’ first and then ‘past’.
I have created a query that works for me, but as soon as I use it in Bricks as a custom query, the posts are sorted randomly.
How to go about it? Thank you
This Pro tutorial shows how to output upcoming events at the top and past events below in a single query loop in Bricks.
Consider this scenario:
CPT: Event
Taxonomy: Event Status
Terms: Upcoming, Past
Default output:

After implementing the tutorial:

Note: There is no ‘Event Date’ custom field in this example. Within each taxonomy group (Upcoming and Past), posts will be set to be shown in descending order of published date.
If you want to instead order events based on the Event Date field automatically without manually assigning them ‘Upcoming’ or ‘Past’ Event Status taxonomy terms, follow the Custom SQL Ordering for Sorting Events in Bricks tutorial instead.
Step 1
Enable query loop on a Block.
Click the query icon and select your Event post type.
Enable PHP query editor and paste:
// Get upcoming event IDs
$upcoming_args = [
'post_type' => 'event',
'posts_per_page' => -1,
'fields' => 'ids',
'tax_query' => [
[
'taxonomy' => 'event-status',
'field' => 'slug',
'terms' => 'upcoming'
]
],
'orderby' => 'date',
'order' => 'DESC'
];
// Get past event IDs
$past_args = [
'post_type' => 'event',
'posts_per_page' => -1,
'fields' => 'ids',
'tax_query' => [
[
'taxonomy' => 'event-status',
'field' => 'slug',
'terms' => 'past'
]
],
'orderby' => 'date',
'order' => 'DESC'
];
$upcoming_ids = get_posts( $upcoming_args );
$past_ids = get_posts( $past_args );
// Merge the arrays while maintaining order (upcoming first, then past)
$all_event_ids = array_merge( $upcoming_ids, $past_ids );
if ( $all_event_ids ) {
return [
'post_type' => 'event',
'posts_per_page' => -1,
'post__in' => $all_event_ids,
'orderby' => 'post__in',
];
} else {
return [
'post__in' => [ 0 ],
];
}
We are using 2 WordPress queries to obtain the post IDs of upcoming events and past events, combining them and setting this combined array as the value of post__in query parameter.