3rd Jan '24
/
4 comments

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 610 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

How to register a new dynamic tag for setting excerpt word limits and outputting an ellipsis (...) at the end.
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
Upcoming Events Grouped by ‘Month Year’ in Bricks

Upcoming Events Grouped by ‘Month Year’ in Bricks

A custom query loop type for showing future events based on a date custom field using three nested query loops.
ACF Repeater Sub Field Values as Tabs in Bricks

ACF Repeater Sub Field Values as Tabs in Bricks

Bricks' query loop supports ACF Repeaters and Meta Box Groups out of the box besides posts, users and terms. This tutorial shows how we can…
Categories:
Tags:
Pro
Querying Posts by Date/Datetime Custom Fields in Bricks

Querying Posts by Date/Datetime Custom Fields in Bricks

This Pro tutorial shows how to configure ACF and Meta Box to query posts in Bricks by custom field(s) of the type Date/Date Time Picker…
Categories:
Tags:
Pro
ACF Relationship Select Filter in Bricks

ACF Relationship Select Filter in Bricks

In the BricksLabs Facebook group, a user asks: I'm learning about ACF relationships and attempting to output a list of posts on any given page,…
Categories: