Fill post query in bricks with meta box relationship posts

When building dynamic WordPress sites with Bricks Builder, there are times when you want to display posts related through a Meta Box relationship field, but also ensure there’s a fallback to show other posts of the same type if no relationships exist. This can be especially helpful for things like testimonials, related articles, or services. […]

When building dynamic WordPress sites with Bricks Builder, there are times when you want to display posts related through a Meta Box relationship field, but also ensure there’s a fallback to show other posts of the same type if no relationships exist. This can be especially helpful for things like testimonials, related articles, or services.

In this post, I’ll walk you through a custom PHP query snippet that does exactly that: it fetches related posts via Meta Box’s relationship field and then fills in the rest of the results with other posts from the same post type — creating a seamless experience for the user and ensuring you never end up with an empty section.


The Goal

We want to:

  1. Query posts related to the current post via a Meta Box relationship field.
  2. Get all other posts of the same post type.
  3. Merge the two, showing related posts first, then fallback posts.

The Code

Here's the full query snippet:

phpCopyEdit$related = new WP_Query([
    'relationship' => [
        'id' => 'review-service',
        'to' => get_the_ID(),
    ],
    'nopaging' => true,
]);

$related_ids = wp_list_pluck($related->posts, 'ID');

$all = new WP_Query([
    'post_type' => 'testimonial',
    'posts_per_page' => -1,
    'fields' => 'ids',
]);

$other_ids = array_diff($all->posts, $related_ids);

return [
    'post_type' => 'testimonial',
    'post__in' => array_merge($related_ids, $other_ids),
    'orderby' => 'post__in',
    'posts_per_page' => -1,
];

Step-by-Step Explanation

1. Query Related Posts via Meta Box Relationship

phpCopyEdit$related = new WP_Query([
    'relationship' => [
        'id' => 'review-service',
        'to' => get_the_ID(),
    ],
    'nopaging' => true,
]);
  • This uses Meta Box’s custom relationship query support.
  • 'id' => 'review-service' targets your Meta Box relationship field.
  • 'to' => get_the_ID() fetches posts connected to the current post.

2. Extract the IDs of Related Posts

phpCopyEdit$related_ids = wp_list_pluck($related->posts, 'ID');
  • wp_list_pluck() helps us extract just the post IDs from the $related query.
  • We'll use these to prioritize related posts in the final output.

3. Fetch All Testimonials

phpCopyEdit$all = new WP_Query([
    'post_type' => 'testimonial',
    'posts_per_page' => -1,
    'fields' => 'ids',
]);
  • This retrieves the IDs of all posts from the testimonial post type.
  • 'fields' => 'ids' makes the query more efficient by returning only the IDs.

4. Exclude Already Related Posts

phpCopyEdit$other_ids = array_diff($all->posts, $related_ids);
  • We don’t want to show the same post twice.
  • array_diff() removes any post already found in the related set.

5. Merge & Return the Final Query

phpCopyEditreturn [
    'post_type' => 'testimonial',
    'post__in' => array_merge($related_ids, $other_ids),
    'orderby' => 'post__in',
    'posts_per_page' => -1,
];
  • We merge both arrays — showing related posts first, followed by the rest.
  • 'post__in' ensures only these posts are queried and displayed in the order we set.
  • 'orderby' => 'post__in' respects the merged array order.

Where to Use This in Bricks Builder

image

This code goes into a custom query in Bricks Builder:

  1. Select your Query Loop element (like a Repeater or Post element).
  2. Under the Query panel, choose "Custom PHP".
  3. Paste this snippet into the code area.

Make sure your Meta Box relationship field ID (review-service) and post type (testimonial) match your actual site setup.

Resources

Recent posts:

web development & web design services moritz reitz

WordPress core concepts

This article is meant to give you a general overview on the most important WordPress concepts & a general overview of how the CMS works.
image

Fluent Forms geoplugin & ipinfo error

The Solution After notfying the Fluent Forms Team they realeased a hotfix withing 24hrs, which I greatly appreciate. Now the service is replaced with Google's API. You can obtain you API key at https://cloud.google.com/. The Issue If your form presents an error such as "Sorry! Please provide valid token for ipinfo.io in global settings" or […]

Postcode Distance Calculator FluentForm (Google Maps Distance Matrix API)

If you want your Fluent Forms WordPress form to automatically calculate travel distance based on a user’s postcode, and pass that distance to your emails or database, this step-by-step guide will help you set it up — using Google Maps Distance Matrix API for accurate distance calculation. Why Use Google Maps Distance Matrix API? Calculating […]

bricks builder specialist

Get in touch
Contact Form
crosschevron-left