Moritz Reitz
← Back to blog
Bricks BuilderMeta Box ProWeb Development

Bricks Builder Meta Box Relationship Query with Post Fallback

A custom PHP snippet for Bricks Builder that queries Meta Box relationship posts first, then backfills with other posts of the same type so your query loop never runs empty.

21 July 2025 · Moritz Reitz

If you're building a dynamic site with Bricks Builder and Meta Box, you'll eventually need to populate a query loop using a Meta Box relationship field — while also making sure the loop never runs empty. This post covers the exact PHP snippet I use to build a Bricks Builder Meta Box relationship query that fetches related posts first, then backfills remaining slots with other posts from the same post type.

It's a pattern that works particularly well for testimonials, related services, or portfolio pieces — anywhere you want curated relationships to take priority but still want content to show when none are set.

What this query does

  1. Query posts related to the current post via a Meta Box relationship field
  2. Fetch all other posts of the same post type
  3. Merge the two arrays — related posts first, fallback posts after

The full PHP snippet

$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,
];

Replace review-service with your Meta Box relationship field ID and testimonial with your actual post type.

Step-by-step breakdown

1. Query related posts via Meta Box relationship

$related = new WP_Query([
    'relationship' => [
        'id' => 'review-service',
        'to' => get_the_ID(),
    ],
    'nopaging' => true,
]);
  • Meta Box extends WP_Query with a relationship argument when MB Relationships is active
  • 'id' => 'review-service' targets your specific relationship field by its registered ID
  • 'to' => get_the_ID() retrieves posts connected to the current post — swap for 'from' to reverse the direction

2. Extract related post IDs

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

wp_list_pluck() pulls just the IDs from the query result. We'll use these to prioritise related posts in the final merge and exclude them from the fallback set.

3. Fetch all posts of the same type

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

Using 'fields' => 'ids' keeps this query lightweight — we only need the IDs at this stage, not the full post objects.

4. Remove duplicates with array_diff

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

array_diff() strips any IDs already present in $related_ids from the full list, so each post appears only once in the final output.

5. Merge and return the final query

return [
    'post_type' => 'testimonial',
    'post__in' => array_merge($related_ids, $other_ids),
    'orderby' => 'post__in',
    'posts_per_page' => -1,
];
  • array_merge() combines both arrays — related posts first, fallback posts after
  • 'post__in' restricts the query to only these IDs
  • 'orderby' => 'post__in' preserves the merged order so related posts always appear first

Adding the query to Bricks Builder

Bricks Builder custom PHP query panel showing the query type dropdown
  1. Select your Query Loop element in Bricks Builder
  2. Open the Query panel and switch the type to Custom PHP
  3. Paste the snippet into the code area
  4. Update the relationship field ID and post type to match your setup

This approach works for any custom post type and is one I use regularly on bespoke WordPress builds where clients need curated post relationships without adding a plugin like JetEngine.

Things to watch out for

  • MB Relationships must be active — the relationship WP_Query argument only works when the MB Relationships extension is enabled, either via the standalone plugin or Meta Box AIO
  • Match your field ID exactly — the id value must match the relationship ID set when registering the field, not the field label
  • Direction matters — 'to' queries posts connected to the current post; use 'from' to query in the opposite direction
  • Performance on large sets — posts_per_page => -1 is fine for small CPTs but consider capping the result if you're working with hundreds of posts

If you're also working with manually reordered posts in Meta Box, the query logic is a little different — I cover that in detail in my guide on querying reordered Meta Box posts in Bricks Builder. For everything else related to custom WordPress builds, see my web development services.

Further reading

FAQ

Does this work without Meta Box Pro? The `relationship` WP_Query argument requires the MB Relationships extension. This is included in Meta Box AIO, or available as a free standalone plugin from the WordPress repository. The core Meta Box plugin alone is not enough.
What happens if no relationship posts are set? If no related posts exist for the current post, `$related_ids` will be an empty array and `array_merge()` will return all posts from the fallback query. The loop will still populate — it just won't have any prioritised posts at the top.
Can I use this with any custom post type? Yes. Replace `testimonial` with your post type slug and `review-service` with your relationship field ID. The logic is post-type agnostic.
How do I find my Meta Box relationship field ID? The relationship ID is set when you register the relationship in PHP or via the Meta Box builder. Look for the `id` key in your `mb_relationships_init` call, or check the Meta Box settings in WP admin if you registered it through the UI.
What's the difference between 'to' and 'from' in the relationship query? Meta Box relationships are directional. If post A is connected to post B, querying `'to' => post_B_ID` returns post A, and querying `'from' => post_A_ID` returns post B. Which direction you need depends on how you set up the relationship and which side the current post is on.

Work with me

Found this helpful?

Get in touch if you need help with your website, SEO, or paid advertising — I offer a free initial consultation.