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
- Query posts related to the current post via a Meta Box relationship field
- Fetch all other posts of the same post type
- 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
relationshipargument 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

- Select your Query Loop element in Bricks Builder
- Open the Query panel and switch the type to Custom PHP
- Paste the snippet into the code area
- 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
relationshipWP_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
idvalue 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 => -1is 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
- Meta Box MB Relationships — querying related objects
- Bricks Builder forum: Meta Box relationship in query loops
- WordPress developer docs: WP_Query class reference