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

Related Tutorials..

How to create filters with IsotopeJS in Bricks (Part 3): apply multiple filters

How to create filters with IsotopeJS in Bricks (Part 3): apply multiple filters

This tutorial will review how to apply multiple filters to an isotope container using the IsotopeJS library‘s features in Bricks.
Pro
Filtering ACF Relationship Query by Post Meta in Bricks

Filtering ACF Relationship Query by Post Meta in Bricks

How to filter the posts of a post type related to another post type based on the value of a True / False type ACF…
Pro
Sorting ACF Relationship Posts by Date Meta in Bricks

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…
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 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: