Consider this ACF field group for a “Publication” CPT:
Field label: Related Posts
Field type: Post Object
Select 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:
<?php
if ( ! function_exists( 'bl_get_related_posts_count' ) ) {
function bl_get_related_posts_count(): int {
return count( get_field( 'related_posts', BricksQuery::get_loop_object()->ID ) );
}
}
Step 4
Whitelist the bl_get_related_posts_count function.
Ex.:
<?php
add_filter( 'bricks/code/echo_function_names', function() {
return [
'bl_get_related_posts_count'
];
} );
You should also add other functions (native or custom) being used in your Bricks instance besides bl_get_related_posts_count. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.
More info on whitelisting can be found here.
Step 5
Edit the Page in which you’d like to have the output with Bricks.
Copy this JSON of fully-built Section and paste.
Outer query loop:

Inner query loop:

Condition on “Related Post” heading:

{echo:bl_get_related_posts_count}
Condition on “Related Posts” heading:

Condition on the inner query loop’s parent (UL):
