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

Related Tutorials..

Pro
ACF Group Sub Field Values from Matching Post Term

ACF Group Sub Field Values from Matching Post Term

In Bricks Reddit, a user asks: Let's break this down and set up the scenario practically. First, there's a Language taxonomy for posts (could be…
Categories:
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
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:
Weekday Condition in Bricks

Weekday Condition in Bricks

As of Bricks 1.7, the built-in "Weekday" condition does not work correctly. This is most likely due to it not taking the site's timezone (set…
Categories:
Pro
Condition to Check if the Current Category Has At Least One Child

Condition to Check if the Current Category Has At Least One Child

Looking to render an element in the category archive Bricks template only if the category of the current category archive page is a parent? This…
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
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)…
Conditionally Outputting Elements only for Posts that have a Specific Taxonomy Term

Conditionally Outputting Elements only for Posts that have a Specific Taxonomy Term

Using the core WordPress has_term() function for checking if the current post has a specific term of a given taxonomy.
Categories: