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..

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:
Pro
Custom Dynamic Data Tags for ACF Field Label, Prefix and Suffix in Bricks

Custom Dynamic Data Tags for ACF Field Label, Prefix and Suffix in Bricks

Bricks lacks built-in dynamic tags for ACF field labels, prefixes, and suffixes. This tutorial shows how to add them using custom dynamic tags.
Categories:
Pro
Conditional Output Based on ACF Date Field in Bricks

Conditional Output Based on ACF Date Field in Bricks

Updated on 1 Aug 2024 Looking to have a Section or other elements in Bricks rendered on the frontend only if today's date is before…
Categories:
Pro
Month and Day Condition in Bricks

Month and Day Condition in Bricks

This Pro tutorial shows how we can define a custom function that takes in a month in the three-letter format (like Dec) and a day…
Categories:
How to apply a custom query var to multiple query loops in Bricks

How to apply a custom query var to multiple query loops in Bricks

In this tutorial, we'll learn how to apply a PHP filter to modify the query var of multiple query loops in a single function Introduction…
Categories: