10th Nov '22
/
5 comments

Meta Box Relationship in Bricks

Updated on 19 Mar 2024

In BricksLabs' Facebook group a user asks:

I have a Product (Metabox CPT) that is connected to a Creator (Metabox CPT) via Relationships.

In the Product single page, I want to list 'Other products from the same Creator' using a loop item.

How do I query this?

From what I see, this is not possible to do using the built-in controls of the Query interface in Bricks.

We need to:

  • get connected creators from the current product
  • get products connected to the above creators
  • remove the current product from the above products
  • modify the query to show only the above products

With post type A to post type B Meta Box relationship, this Pro tutorial provides the steps to use Meta Box' API for querying items of post type B connected to the current item of post type A (forward query), then doing a backward query to get all items of post type A connected to the matching post type B items and removing any occurrences of current post type A items to finally pass them to Bricks' bricks/posts/query_vars filter.

In the Meta Box documentation, the example provided is Posts connected to Pages.

Per the original question, the relationship is: Products connected to Creators.

In the example below, we are going to do Events connected to Testimonials (I happen to have entries for these already present in my local test site and just went with this setup).

Here is the notes I put together of the problem statement:

Problem Statement

Relationship title: Event <-> Testimonial

Relationship ID: event-testimonial

Event 1 (2431): Frances Harper (2811)

Event 2 (2432): James Colley (2809) and Alicia Burke (2807)

Event 3 (4263): Frances Harper (2811) and James Colley (2809)

Event 4 (4264): Alicia Burke (2807)

Numbers in parentheses indicate the item ID.

Objective: In the Event single page, list other events having the same testimonial.

When viewing Event 3 which has testimonials by Frances Harper and James Colley, for example, show all other events that have testimonials by either of them i.e., Event 1 and Event 2.

We will also get the count of the matching events and ensure that the Section in which we have the Posts element (which could also be a Query Loop instead) only outputs if there is at least 1 event to be shown.

Step 1

Install and activate Meta Box and Meta Box AIO.

Create your two post types.

Go to Meta Box → Relationships and create one.

Step 2

Create a few testimonial entries:

and some event entries:

Step 3

Add a new Bricks template named "Single Event" of the type "Single" and edit it with Bricks.

Click the Settings (gear) icon → TEMPLATE SETTINGS → CONDITIONS and add a Post type = Events.

Add a Section and inside its Container, Post Title and Post Content elements.

Add another Section and inside its Container, a "Related Events" Heading.

Set up a Query Loop.

Click the Query loop icon and enable the PHP query editor.

Paste:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 628 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Dynamic Source for Video Element in Bricks Using Meta Box Post Field

Dynamic Source for Video Element in Bricks Using Meta Box Post Field

This Pro tutorial provides the steps to set the source of a Bricks video element on single CPT pages to be the URL of a…
Categories:
[Function] Current Term Has Children

[Function] Current Term Has Children

Updated on 20 Jul 2024 A user asks: I'm working on an archive template for WooCommerce products. I'm trying to hide a section if there…
Pro
Meta Box/ACF Images Staggered Grid Gallery in Bricks

Meta Box/ACF Images Staggered Grid Gallery in Bricks

Showing images from two Gallery-type of ACF or Image Advanced-type of Meta Box fields in a staggered grid layout.
Render an element having a specific HTML ID based on value of a custom field

Render an element having a specific HTML ID based on value of a custom field

This tutorial provides the steps to use Bricks' bricks/element/render filter to conditionally output an element that has the specified HTML ID based on the value…
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
Conditional Related Posts in Bricks

Conditional Related Posts in Bricks

How to output a section only if there is at least 1 related post for the current post being viewed.
Categories:
Checking if Repeater/Cloneable Meta Fields Are Empty in Bricks

Checking if Repeater/Cloneable Meta Fields Are Empty in Bricks

Creating a condition to ensure that a section only gets output if at least one row of data is filled in.
Categories:
Pro
Meta Box Related Project’s Featured Image URL on Single Reviews

Meta Box Related Project’s Featured Image URL on Single Reviews

Updated on 26 Jun 2025 In a recent website I worked on, a requirement was to get the URL of the featured image of a…
Pro
Conditional CSS in Bricks based on Post Meta

Conditional CSS in Bricks based on Post Meta

In the Bricks Facebook group a user asked: Hi, how do you normally control margin, padding and font-size using dynamic data? I'm looking for the…