30th Jul '24
/
0 comments

ACF Post Object Query Loop inside a Posts Query Loop in Bricks

Consider this ACF field group for a "Publication" CPT:

Field label: Related PostsField type: Post ObjectSelect multiple: on

Each Publication CPT post can have 0 or 1 or more related posts.

The objective is to loop through the publication posts and within each publication, output a list of related posts. Like this:

The power of Bricks means such nested querying is doable without writing any code.

But Bricks' query result count dynamic tag does not work in nested loops. This means it is not possible out of the box to show conditional headings of "Related Post" and "Related Posts" based on the number of related posts. Also, you would want the related posts list itself be output only if there is at least 1 related post so there is no empty markup.

This Pro tutorial provides the steps to set up the above nested query loops, define a custom function that returns the count of related posts and using this function for conditional output.

Step 1

Create your field group.

While it should not matter, note that we set the return format to Post ID in our dev site.

Step 2

Add sample posts and populate the field.

Step 3

Add the following in child theme's functions.php (w/o the opening PHP tag) or a code snippets plugin:

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
Term Parent Slug Data Attribute in Bricks

Term Parent Slug Data Attribute in Bricks

How we can set a data attributes on a child taxonomy dynamically based on the parent taxonomy.
Categories:
How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

This tutorial provides the PHP & JS codes that can be pasted in order to create a flying effect on map markers each time your…
Pro
Post ID Query Variable-based Post Output in Bricks

Post ID Query Variable-based Post Output in Bricks

How to query a single post of a custom post type using the post ID passed to a page via a URL parameter.
Pro
Adding active class to the current term/post in a Bricks query loop on Term archives/single posts

Adding active class to the current term/post in a Bricks query loop on Term archives/single posts

Updated on 29 Feb 2024 In the Sibling Terms Bricks Query Loop tutorial, a member asked: How can I set the class "active" to the…
Categories:
Pro
How to Insert Element(s) Between Query Loop Posts in Bricks

How to Insert Element(s) Between Query Loop Posts in Bricks

Update on 16 Aug 2023: Follow this tutorial instead. This Pro tutorial shows how we can insert a Div (or any custom HTML) after nth…
Modifying ACF Field Value Before It Is Output

Modifying ACF Field Value Before It Is Output

Consider the scenario where a Page has a "Page Fields" field group created with ACF Pro like this: Field name: scientific_coordinators (type Repeater)|__ Sub field…
Categories:
Loop Item ID and Loop Item Object in Bricks

Loop Item ID and Loop Item Object in Bricks

If you are working on a Bricks site with specific requirements, chances are you need to grab the ID and/or object of the current item…
Categories: