28th Feb '25
/
9 comments

Filtering Bricks Query Loop By Meta Box Relationship

In the Bricks Facebook group a user asks:

Display based on relationship

I'm trying to run a query loop that only outputs posts that have a value in their relationship. So there are Services Areas and City Pages. Some of the Service Areas do not yet have City Pages written for them, but will be adding over the next few months.

I want my query loop to only output Service Areas that have a related City Page. I can't seem to make it work.

Consider this setup:

CPTs: Service Areas and City Pages

Meta Box Relationship: City Pages to Service Areas

A Service Area post can have 0 or 1 or more City Page(s).

Sample Service Area with some City Pages:

Sample Service Area with no City Pages:

The objective is to only output the Service Area posts that have at least 1 connected/related City Page.

Service Areas query loop by default:

Service Areas query loop after implementing this Pro tutorial:

Steps in brief:

In the Bricks Query Loop PHP editor

  1. Get all service area IDs.
  2. Loop through each service area and for each, check if there is at least 1 connected city using MB's Relationship API. If so, store the service area ID in an array.
  3. Set post__in to the above array.

Step 1

Install and activate Meta Box and its AIO extension.

Create both the CPTs and a relationship.

Step 2

Create a few Service Area posts whilst selecting the related City Pages as applicable.

Step 3

Edit a Page or a Template and set up a query loop on a Block.

Select Service Areas as the Post type.

Enable PHP query editor and paste:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 632 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Bricks Dynamic Condition – Check if today falls within 5 days before any holiday date

Bricks Dynamic Condition – Check if today falls within 5 days before any holiday date

Creating a custom condition that returns true or false depending on whether today falls within 5 days before any holiday dates selected on a Meta…
Categories:
Tags:
Color Custom Field as Section Background Color on Category/Term Archives in Bricks

Color Custom Field as Section Background Color on Category/Term Archives in Bricks

How to use a color from a taxonomy term custom field for as the hero section background color on archive pages.
Categories:
Tags:
Pro
Ordering Meta Box group rows by a sub field in Bricks

Ordering Meta Box group rows by a sub field in Bricks

In the BricksLabs Facebook group a user asks: Any idea how to set the sort order of a cloneable group in the query? In this…
Pro
HappyFiles Folder Images via Meta Box Taxonomy Field in Bricks Image Gallery

HappyFiles Folder Images via Meta Box Taxonomy Field in Bricks Image Gallery

How to set the source of Bricks’ Image Gallery element on a post to images in the selected HappyFiles Folder(s) via a Meta Box Taxonomy…
Pro
Related Episodes Grouped by Episode Chapters on Single Podcasts in Bricks when using JetEngine

Related Episodes Grouped by Episode Chapters on Single Podcasts in Bricks when using JetEngine

A user asks: How to Display Related Episodes Grouped by Chapters on a Podcast Page in Bricks? Hi everyone, I have two post types created…
Pro
Meta Box Checkbox List Query Type in Bricks

Meta Box Checkbox List Query Type in Bricks

This Pro tutorial provides the steps to add a new query type in Bricks builder for displaying the values of a Checkbox List type of…
Categories:
Pro
Querying Posts by Date/Datetime Custom Fields in Bricks

Querying Posts by Date/Datetime Custom Fields in Bricks

This Pro tutorial shows how to configure ACF and Meta Box to query posts in Bricks by custom field(s) of the type Date/Date Time Picker…
Categories:
Tags: