Nested query loop: Custom Taxonomies within Custom Post Types

How do we exacty the opposite, where we want to pull in an image from an acf field in the taxonomy for a post? I can’t get it to happen. We have a product (CPT), which has a brand taxonomy (ACF Taxonomy), which has several field groups, one of which being the “brand_image_svg”. For the life of me, I cannot get that image to show, nor any other of that post’s “Brand” taxonomy fields. I have tried to use sub loops, but nothing I do to restrict to the one “Brand” assigned to the product works. I can get it to display fields from the entire Brand taxonomy in a loop, but specifying it to just the one specific “Brand” taxonomy term on the “Product” post has proven impossible.

UPDATE: SOLVED
I finally got it! For anyone in the same boat, the nested loop for getting terms fields under a post is like this:

Imagine you want to pull in the “Brand” logo from the “Brand” taxonomy assigned to a “Car” CPT.

  1. Outer loop - Posts (I chose a CPT of mine called “Cars” in my example below)

  2. Inner loop inside the post loop - Terms (I chose a custom ACF taxonomy i created called “Brands” in my example below)

  • Set “number” of terms to return to 1 returned (in my case, only one “Brand” to each “Car”).
  • The meta filter needs to have something in common between the term and the post, OTHER THAN the term_name. For my example, I added an ACF field for the “Cars” CPT store the “Brand Name” as a text field “cpt_car_brand_name”, and I added the same field to the Taxonomy as an ACF test field and called it “tax_brand_brand_name” that was set to a value identical to the “Brand” taxonomy term_name associated with car as its Brand taxonomy. Eg. If the Brand term name was “Audi”, I stored the “Audi” in the “Car” CPT ACF field “car_brand_name”. Then, in “Meta Query” of the Taxonomy query, set “Meta Key” to “tax_brand_brand_name” and Meta Value to “{acf_cpt_car_brand_name}”

Ahhhhh. Success.

I truly hope this saves someone hours and hours in the future :slight_smile:

2 Likes