ACF Relationship: Show all other events related to the artists of the current event

Consider the following scenario when using ACF in Bricks:

CPTs: event and artist.

Event CPT has a Relationship field named event__related_artists.

Artist CPT has a Relationship field named artist__related_events.

Both fields are bidirectional.

This Pro tutorial shows how all other events related to the artists of the current event can be output in a Bricks query loop.

Event CPT field group

Artist CPT field group

Note that both the relationship-type fields’ return format is at the default, Post Object.

Setting the fields to be bidirectional

Editing an event post

Sample data

Artist 1: Event A

Artist 2: Event B, Event C

Artist 3: Event C, Event A

After implementing the tutorial

From the sample data we can see that Event C is related to Artist 2 and Artist 3.

The events related to Artists 2 and 3 are Event A, B and C.

When Event C is being viewed on the front end, the output shows Events A and B as expected and Event C is set to be excluded since it is the current post.

Step 1

Create both your CPTs and their corresponding field groups.

Create the Relationship-type fields in each and enable bidirectional relationships.

Edit the posts of any of your post types and select the related post of the other post type.

Step 2

Create and edit a Bricks template called “Event” (going by the current example).

Add a template condition so it applies to singular posts of event CPT.

Set it up like this:

For the query loop, choose Events post type.

Enable PHP editor and paste:

// Get related artists' IDs for the current event
$related_artists = wp_list_pluck( get_field( 'event__related_artists' ), 'ID' );

if ( $related_artists ) {
	$related_event_ids = [];
	
	// Loop through each related artist and get their related events
	foreach ( $related_artists as $artist_id ) {
		// Get IDs ofrelated events
		$artist_related_events = wp_list_pluck( get_field( 'artist__related_events', $artist_id ), 'ID' );
		
		if ( $artist_related_events ) {
			foreach ( $artist_related_events as $related_event_id ) {
				// Exclude the current event from the results
				if ( $related_event_id !== get_the_ID() ) {
					$related_event_ids[] = $related_event_id;
				}
			}
		}
	}

	// Remove duplicates
	$related_event_ids = array_unique( $related_event_ids );

	return [
		'post_type' => 'event',
		'posts_per_page' => 100, // a large number
		'no_found_rows' => true,
		'post__in' => $related_event_ids,
		'orderby' => 'post__in',
	];
} else {
	return [
		'post__in' => [ 0 ],
	];
}

Change the field names and CPT to match your setup.

The Basic Text element’s text has been set to:

{post_title:link}

Step 3

Apply a dynamic data condition on the Section like this:

Replace zrbred with the Bricks ID of the query loop-enabled element.

This ensures that the Section is rendered only if there’s at least 1 query result.