Sorting ACF Relationship Posts by Date Meta in Bricks

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 usort function to sort the $result array. 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 $a should come before $b or a positive number if $a should come after $b or 0 if the order doesn’t matter (they’re equal).
  • In the comparison function,
    • we use ACF’s get_field() function to retrieve the event_start_date field for each post. We use the full field name event_date_information_event_start_date because 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.
  • After sorting, we return the modified $result array.
  • If you want descending order (latest date first), simply change $timestamp_a$timestamp_b to $timestamp_b$timestamp_a.