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

Related Tutorials..

Pro
Filtering Cloneable Meta Box Group by Sub Field Value in Bricks

Filtering Cloneable Meta Box Group by Sub Field Value in Bricks

This Pro tutorial provides the steps to filter the rows of a Meta Box cloneable group to only those that have a date custom field…
Pro
Filtering posts by a group field’s true / false field sub field value with ACF or Meta Box in Bricks

Filtering posts by a group field’s true / false field sub field value with ACF or Meta Box in Bricks

This Pro tutorial covers how posts of a Bricks query loop can be filtered to show only the ones whose true / false or checkbox…
Categories:
Tags:
Custom Image Field Avatar in WordPress

Custom Image Field Avatar in WordPress

Learn how to use a Meta Box image field value as the author profile image on single post pages in WordPress/Bricks.
Categories:
Tags:
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
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
Meta Box Relationship Posts Filtered by a Taxonomy Term in Bricks

Meta Box Relationship Posts Filtered by a Taxonomy Term in Bricks

Updated on 17 Feb 2025 In the Inner Circle a user asks: Hello, I have a relationship made in metabox between two Post Types “Travel”…
Categories: