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 626 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

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 Date Field Value Custom Format in Bricks

Meta Box Date Field Value Custom Format in Bricks

Updated on 3 Apr 2024 Note: A custom function is no longer needed. Use the date format filter, as mentioned in the Bricks documentation. Ex.:…
Categories:
Tags:
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
Meta Box Relationship in Bricks using Posts Query Loop

Meta Box Relationship in Bricks using Posts Query Loop

This Pro tutorial is similar to the recent ACF Relationship in Bricks using Posts Query Loop guide but for Meta Box. In the past, we…
Categories:
Pro
Sorting ACF Relationship Posts by Date Meta in Bricks

Sorting ACF Relationship Posts by Date Meta in Bricks

Consider the following setup: CPT: Events ACF Fields:Event Date Information (Group)|__Event Date (Date Picker)Pick Sub Events (Relationship) An Event can have 1 or more related…
Pro
Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

How to register a new dynamic tag for setting excerpt word limits and outputting an ellipsis (...) at the end.
Categories:
Pro
ACF Relationship: Show all other events related to the artists of the current event

ACF Relationship: Show all other events related to the artists of the current event

How all other events related to the artists of the current event can be output in a Bricks query loop.
Categories: