3rd Mar '25
/
0 comments

Host Post’s Term in Bricks Query Loop

Updated on 4 Mar 2025

In the BricksLabs Facebook group a user asked:

Hi everyone, I’m facing a tricky situation that might have a simple solution, but I’m having a hard time wrapping my head around it. I would appreciate your assistance!

How can I fetch dynamic data from 2 different CPTs while querying only one CPT?

I have a template for my Custom Post Type (CPT) called "Areas."Within this template, I’m trying to query another CPT called "Programs."

Each Program is linking to a custom URL structured like this:{mb_programs_link}&_area={mb_areas_term:plain}

The value for {mb_programs_link} is being fetched correctly.However, I’m having trouble with {mb_areas_term:plain}; it’s not outputting any value at all.

I suspect the issue arises because {mb_areas_term} is defined in the CPT "Areas," while I’m trying to access it from the CPT "Programs."

{mb_programs_link} is defined in the CPT "Programs" which I am querying in that section, so this works.

Is my understanding correct? How can I resolve this issue?

Thank you for your help

Consider this scenario:

CPT: programCustom field: program_link (text)

CPT: areaTaxonomy: area-group

Each area post has only 1 area-group term at most.

View in question: Single area post pages.

Inside the Program CPT query loop, an element (let's say post title) should be linked to the value of program_link custom field followed by &_area= and the area-group taxonomy's term slug for the current area.

Screenshots aftrer implementing the tutorial:

The challenge here is that since we are inside the program CPT's loop, any attempt to fetch the area-group term is going to fail because that taxonomy is attached to the host area CPT and not the program CPT that are are looping through.

We tackled this core problem in the past tutorial titled Current Single Post Reference From Inside a Bricks Query Loop.

This Pro tutorial provides the steps to register a custom function that returns the term name of a specified taxonomy for a single post of a specified post type and use that function to pull the (first and only, in this case) term slug of the host post.

Step 1

Register these two CPTs: area, program.

Register area-group taxonomy for area CPT. Set it to be hierarchical. Create a few sample terms. Add an Area CPT post and select one Area Group.

Add a field group for Program CPT having a text (or URL) type field with the ID of program_link.

Add a few Program CPT posts and for each, set the Program Link.

Step 2

Add the following in child theme's functions.php (w/o the opening PHP tag) or a code snippets plugin:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 610 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Conditionally Rendering an Element Outside the Loop based on Taxonomy Term in Bricks

Conditionally Rendering an Element Outside the Loop based on Taxonomy Term in Bricks

A user asks: Hiding element based on taxonomy I have an element in a footer that I want to hide if a page has a…
Categories:
Dynamic Data :value filter in Bricks

Dynamic Data :value filter in Bricks

Bricks 1.5.7 introduced a new :value dynamic data filter. This tutorial provides a practical example of how this filter can be used. Meta Box Checkbox…
Categories:
Pro
How to Insert Element(s) Between Query Loop Posts in Bricks

How to Insert Element(s) Between Query Loop Posts in Bricks

Update on 16 Aug 2023: Follow this tutorial instead. This Pro tutorial shows how we can insert a Div (or any custom HTML) after nth…
How to List Your Posts Divided by Categories in Bricks

How to List Your Posts Divided by Categories in Bricks

In this tutorial, we'll learn how to display a list of posts divided by each category. The DOM tree Here is how I structured my…
Categories:
Pro
Custom Dynamic Data Tags in Bricks

Custom Dynamic Data Tags in Bricks

Updated on 4 Mar 2025 In the BricksLabs Facebook group a user asked: Hi everyone, I’m facing a tricky situation that might have a simple…
Pro
ACF Repeater sub field value dynamic data condition in Bricks

ACF Repeater sub field value dynamic data condition in Bricks

This Pro tutorial provides the steps to output an element on single posts (can be of any post type) only if a specified ACF (Pro)…
Month-based Background Image in Bricks

Month-based Background Image in Bricks

How to have different background images for an element for different quarters in a year.
Categories: