Consider the following setup:
CPT: Events
ACF Fields:
Event Date Information (Group)
|__Event Date (Date Picker)
Pick Sub Events (Relationship)
An Event can have 1 or more related events via the Relationship field.
When the ACF Relationship is selected as the query type of a Bricks query loop, the event posts will be ordered by their post editor visual order by default. There are no query controls to set the post order with a Relationship type of (and a few other types) of query in Bricks.
The objective is to order the posts by event date in ascending order.
This Pro tutorial shows how this can be done using the bricks/query/result filter.
Step 1
Note: The return format of the date field does not matter and can be any (like m/d/Y). Internally, it gets saved in Ymd format in the database.
Set up a ACF Relationship query loop.

Step 2
Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:
<?php
add_filter( 'bricks/query/result', function( $result, $query_obj ) {
if ( $query_obj->element_id !== 'ihpheu' ) {
return $result;
}
// Sort the result array based on the ACF event_start_date field
usort( $result, function( $a, $b ) {
$date_a = get_field( 'event_date_information_event_start_date', $a->ID );
$date_b = get_field( 'event_date_information_event_start_date', $b->ID );
// Convert dates to timestamps for comparison
$timestamp_a = strtotime( $date_a );
$timestamp_b = strtotime( $date_b );
return $timestamp_a - $timestamp_b; // Ascending order
} );
return $result;
}, 10, 2 );
Replace ihpheu with the Bricks ID of the query loop enabled element.
Since event_start_date field is inside a group named event_date_information, we refer to it as event_date_information_event_start_date.
That’s it. Check on the front end.
Code Explanation:
- We use PHP’s
usortfunction to sort the$resultarray. This function enables us to do a custom comparison while taking two post objects in the array and gets repeatedly called with different pairs of elements from the array. The comparison function should return a negative number if$ashould come before$bor a positive number if$ashould come after$bor 0 if the order doesn’t matter (they’re equal). In the comparison function,- we use ACF’s
get_field() function to retrieve theevent_start_datefield for each post. We use the full field nameevent_date_information_event_start_datebecause it’s inside a group field. - we convert the dates to timestamps using
strtotime()for easy comparison. - we return the difference between the timestamps. This will sort the array in ascending order (earliest date first). By returning
$timestamp_a–$timestamp_b, we ensure that posts with earlier dates come first in the sorted array.
- we use ACF’s
- After sorting, we return the modified
$resultarray. - If you want descending order (latest date first), simply change
$timestamp_a–$timestamp_bto$timestamp_b–$timestamp_a.