Updated on 20 Jun 2024
This Pro tutorial provides the steps to modify the query of a Bricks Posts element or a Block on which query loop has been enabled to limit the posts to only those that are related to the current single post via an ACF Post Object custom field.

Field being populated when editing a Post:

On the front end:

We shall ensure that the
- Section gets output only if there is at least one related post for the current post
- “Related Posts” heading is output if there is only one related post and “Related Posts” heading if there is more than one related post
Step 1
Create the ACF field group having a Post Object type of field named related_posts.
Set “Filter by Post Type” to your desired post type(s) items that you want to set a relationship to.
Set it to appear on the post type of your choice.
In this example, both of the above are set to post.
Ensure that the Return Format is set to Post ID.
Enable “Select Multiple”.
Step 2
Edit your posts and select 1 or more related posts for your posts.
Step 3
Edit your single post template with Bricks.
Copy and paste the Section using this JSON.

Ensure that the Section has this condition:

{query_results_count:vzlhrj}
Replace vzlhrj with the Bricks ID of the query-loop enabled element.
‘Related Post Heading’ condition:
Similar to above but the operator should be ==.

‘Related Posts Heading’ condition:
Similar to above but the operator should be >.

Query loop-enabled Block element’s PHP query:
$related_posts = get_field( 'related_posts' );
if ( $related_posts ) {
return [
'post_type' => 'post',
'posts_per_page' => 100, // a large number
'no_found_rows' => true,
'post__in' => $related_posts,
'orderby' => 'post__in',
];
} else {
return [
'post__in' => [ 0 ], // force WP_Query to return no results
];
}