ACF Relationship in Bricks using Posts Query Loop

Bricks includes ACF Relationship type of query out of the box for showing posts (on single pages of a post type) related via a ACF Relationship field to another post type.

For more flexibility, though, you might want to use a posts query loop instead. This Pro tutorial shows how.

We shall work with "Game" and "Review" CPTs in this example.

Step 1

Create both your CPTs at ACF → Post Types.

Create field groups for each having a Relationship type of field.

Game CPT field group:

Game CPT's custom fields group

Review CPT field group:

Review CPT's custom fields group

Game CPT field group:

Enabling bidirectional relationship with a field attached to Review CPT

Review CPT field group:

Enabling bidirectional relationship with a field attached to Game CPT

Here we created a Relationship type field called related_reviews for game CPT and set this field to bidirectionally link to related_games field of the review CPT.

Similarly, a Relationship field called related_games has been added for the review CPT which is bidirectional with game CPT's related_reviews field.

Step 2

Let's work on showing related reviews when a single game post is being viewed on the front end.

Here's the exported template from our dev site. You can import this and apply a template condition to apply it to all singular posts of the game CPT. You should also select the Block element on which query loop is enabled, click Sign code and Save.

Alternatively, the manual steps are below.

Create a template titled "Game" of type "Single".

Edit it with Bricks and apply a template condition to make it apply to all singular posts of the game CPT.

Add a Section showing Post Title and Post Content.

Add another Section for showing the related reviews.

Add a "Related Review" Heading inside this Section's Container.

We shall programmatically add s at the end of the heading text if there's more than one related review.

Add Container > Block > Basic Text.

Enable query loop on the Block.

Toggle Query editor (PHP) on and paste this PHP:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 526 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Filtering Query Loop Posts by a Non-empty Custom Field in Bricks

Filtering Query Loop Posts by a Non-empty Custom Field in Bricks

This Pro tutorial shows how we can prefilter the results of a query output by a Bricks query loop to only those for which the…
Categories:
Tags:
Pro
Conditional Output based on Date Time Picker Field in Bricks

Conditional Output based on Date Time Picker Field in Bricks

In the past, we showed how elements can be conditionally output based on a post's Date type of ACF field here. This Pro tutorial for…
Categories:
Pro
Testimonials Slider in Bricks

Testimonials Slider in Bricks

Setting up a dynamic testimonial slide that pulls the data from a CPT's post fields and custom fields.
Categories:
Tags:
Pro
Conditional Rendering of ACF Repeater Rows Based on True / False Sub Field in Bricks

Conditional Rendering of ACF Repeater Rows Based on True / False Sub Field in Bricks

This Pro tutorial provides the steps to modify a ACF Repeater query loop to only output the rows for which a True / False type…
Categories:
ACF Checkbox Items with Custom SVG Bullets in Bricks

ACF Checkbox Items with Custom SVG Bullets in Bricks

We walk through outputting list items with SVG icons based on the choice of a ACF Checkbox field.
Categories:
Tags:
Pro
ACF Font Awesome Field in Bricks

ACF Font Awesome Field in Bricks

We walk through using the Advanced Custom Fields: Font Awesome Field plugin for showing Font Awesome icons.
Categories:
Tags:
Pro
ACF Taxonomy Field Value in Single Posts

ACF Taxonomy Field Value in Single Posts

Consider this scenario: CPT: Movie Movie CPT Taxonomy: Movie Genre Movie Genre Taxonomy's field: Genre Color Each movie will have only genre set. The requirement…
Categories:
Tags: