Nested query loop: Custom Taxonomies within Custom Post Types

Hi,

There have been a few recent posts about V1.5 beta and ACF repeaters working but I am still struggling with my use case.

I have a loop of Custom Post Types and within each I want to loop over Custom Taxonomies attached to them. For example, each staff member has multiple skills. I want to loop over the skills and style them.

I tried looping the taxonomy within each record since V1.3.7 but it just displays all terms. It seems to be no different in V1.5 beta.

Should it work? If not, do I need to create a custom shortcode to do it?

Thanks in advance,
James

2 Likes

I’m having the same issue.

Within a custom post type I’m trying to query loop over the taxonomies, but it’s showing all taxonomies, not just the associated taxonomies to the post.

You have to use {post_terms_category} as the meta field key inside the loop.
post_terms_category

If you can decipher this image, it explains what’s happening in my case.

The expected result is that only some of the images (custom fields in custom taxonomy) would appear for each entry item. Right now all images are appearing for all entries.

Ok, the way I understand your picture…

You loop over all posts. Then within each found post, you loop again over all posts within category Platforms.

What dynamic key have you defined in the “Image” under Platforms?

I don’t think you actually need 2 loops.

You can define the key directly in the first (and only) loop. See my screen shot for reference.

I have a loop over the post type “rezepte” (screen shot 1). And within that query I added a text field called “Text post category” in which I defined the dynamic data key to be “{post_terms_category}” (screen shot 2). Note: I don’t have an image in my categories, that’s why I use a text field.

As a result, all posts within post type “rezepte” are shown and all the included category/categories per post are shown.

Hopefully mx explanation helps.

Cheers

Patric

1 Like

Thank you so much for the input so far!

I tried to only have 1 query loop but I didn’t quite get that to work.

Here’s a 2 min video with my setup and how everything is:

VIDEO <<

Thank you

Ok, a complicated case. Maybe I understand it now.

I think the problem is that your second query loop is not connected to your first query loop, meaning no found data of the 1st query loop is used as a query loop parameter for the 2nd query loop.

Your first query loop finds all posts that have the post type GTM Templates. Your 2nd query just finds all items that are in the taxonomy Platforms.

I have a case, where I use a 2nd query loop to filter the posts that have the same category as the posts found in the 1st query loop.

I did this by using the taxonomy found in the 1st query loop as a parameter in the 2nd query loop. This was achieved by using the {post_terms_category} parameter (see the screen shot for reference).

Here you find all the available post parameters like that:

Under taxonomy, they list the following dynamic tags of the taxonomy terms assigned to a post:

{post_terms_category}
{post_terms_post_tag}
{post_terms_my_taxonomy_slug}

I hope that helps.

Cheers

Patric

Okay, I’ve tried this and some variations of this.

If I apply a taxonomy filter to the first query, it no longer returns all the posts - rather it returns the posts that meet the taxonomy filter.

The second query looks for taxonomies, but I can’t figure out how to only get the taxonomies of the given previous query.

You don‘t have to touch / change the 1st query loop.

Ok. it is difficult for me to check this without having access to the data myself.

Anyway, the information in this post might help you:

Check especially this chapter

  1. Posts query loop inside a Terms query loop

which I think covers a similar case as yours.

Also have a look at this one, which might even be easier for you:

Cheers

Patric

1 Like

Did you manage to achieve your queries ?

Here is what I am trying to do:

I have a custom Post Type of Communities

Taxonomies with the following:

  • County
  • City
  • Zip

So just to be clear each Custom post Type Entry has each of the above filled out:

For example:

Custom Post Type Communities

Post title = Seattle

City = Seattle

County = West

Zip Code = 93899

I have hundreds of Communities with all different cities, counties zip codes etc.

On the bricks community template I am trying to do the following:?

When a page loads for communities I want to take the County of the page:

And grab all unique values for city, and Zip code where county = Seattle

Hi, @james!

I encountered the same problem. And I found a solution for it. But I solved it with a little bit of PHP code.

My scenario:
For my portfolio website I create a custom Post Type called Projects and in this post type, I’m showing different technologies used to develop that particular project.

In bricks builder, I used Query Loop to show All Projects. But I encountered problem when I tried to show each technology in a different span tag to style. Becuase, I didn’t find out solution for nested query.

Then, I used a PHP to show the technologies within the query Loop.

I’m also sharing a screenshot and PHP code. I used span tag you can use img tag to solved your problem.

Code:

<?php $technologies = get_field('technologies'); // get the list of technologies from ACF if( $technologies ) { foreach( $technologies as $technology ) { echo '' . $technology . ' '; // wrap each technology in a span tag } } ?>
1 Like

Thanks for your post @muhammadadeel :+1:

1 Like

This one was a Godsend Patric. Thank you.

I’ve been trying to find a way to do that for ages now. It seems surprisingly hard to find good tutorials on that. I’ve been searching for ‘nested’ and ‘grouped’ lists/query loops - which is what found me this post here.

It would be great if the Bricks team (hi chaps) could add a few more examples about this direct to the academy pages (or anywhere) although now I’ve seen that BricksLabs example (which is exactly my use case, I was trying to create a ‘simple’ Product List (of Woo products) grouped (or nested by) product categories, which now seems a lot easier to understand (with hindsight).

And btw Bricks team… brilliant job on this option/logic - now that I’ve unlocked how to build them/ It’s a stunning achievement.

1 Like

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

I have used a similar solution, using Happy Files Folders selected through a Select Advanced field (Metabox) and it only requires one query loop via the taxonomy query for the custom field with the value of the folder. It works great, but perhaps there is merit to having the option to tick a box for a taxonomy right there in the post…rather than relying on a separate step of grouping in folders?

One thing I have run into is where the option to select taxonomy exists in the post AND exists as an option through the field, the two conflict. That may have been due to a required field setting, I don’t recall.

Did you encounter any conflicts? IF so, how did you get around that issue? Did you hide the taxonomy in the sidebar and ONLY allow it in the field set…or some other solution?

Thanks for any response to this older, but informative and helpful thread.

I was able to implement the original scenario without custom PHP code so I’m leaving this for anyone looking for solution using the current version of Bricks (1.11.1.1).

Task

Querry (custom) taxonomy inside (custom) posts loop.

Result

This is a bare bones result you will get

Querries setup

CPT loop

The “CPT loop” element has a basic setup like this. Of course you should select your own CPT in place of my “Portfolio”. Of course you can loop standard Posts as well.

Terms loop

The “CTax loop” element has a Terms qurry. Of course you should select your own CTax in place of my “Użycia tekstu (Portfolio)”. Of course you can loop standard Post Tags as well. The most important setting is in the bottom left corner which is a toggle with “Current post terms”. This option is available since Bricks 1.8.4 and is described in the Docs in the last paragraph here: Query Loop – Bricks Academy

Customization

For a quick showcase I’ve used the {post_id} and {term_id} but in a real life scenario those would get replaced by {post_title}, {term_name} or any custom fields attached to them.

@charaf could you or anyone from team consider adding an example in the mentioned Docs. Currently there’s a showcase of “Terms” query in Example 3, yet I believe that many people would appreciate the example of nested “Terms” inside of “Posts” in one or many following setups:

  1. Posts loop with Tags loop inside
  2. Products loop with product taxonomies/categories
  3. Any CPT with any CTax (like property listings, portfolio with technology stack used, etc.)

Assuming that the inner loop gives more than one result, which would showcase how powerful Bricks is in terms (pun unintended) of nested loops setup.