Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Christopher Paccione
Christopher Paccione
14,831 Points

WordPress AJAX category/tag filter

I'm in the process of building a product page that will not be part of woocommerce for WordPress, because the client sells big industrial products that are not purchased over the internet that said my structure goes like this.

Custom Post type using Custom Post Type UI ACF with repeater field Two templates, one for the product grid and one for the click through for the product.

I need to create a tag filter using AJAX, because I want the user to be able to simply add a product category when they create the post, and then I will pull that category to it's proper page using WP_Query, but then on that page I want to use tags to create a filter using AJAX.

That's where I'm stuck. I figured out through research and scouring Google how to create a function and display all tags on a page using AJAX, but I now want to figure out how to display only tags of a particular category on that page, and load all the tag categories first and then let the user sort through them if that makes sense.

I'm posting this problem, because I would really like to see Zak have a workshop on an advanced topic like this.

If anyone has any suggestions or advice in the meantime I'm all ears, and I will post my code if anyone wants to take a look.

3 Answers

Hi Christopher,

Firstly can I ask what is the reason for needing to use AJAX in the case?

As far as I know Zac has moved on to another project now but if you really get stuck and you have the budget for it codeable would always be able to help.

It does seem to be that WordPress has the functions to do what you are asking natively it would simply be a case of getting the process of adding the posts and creating the categories / tags correct to be able to use them.

There is no reason why you cant use the global $post to return only the categories associated with that post type as well as the tags to.


Christopher Paccione
Christopher Paccione
14,831 Points

Thanks for the response Craig.

I wanted to use ajax, because I was looking for a way to create a filter that further sort through the category displayed on the page with WP_Query. I figured it would be easier for the client to add a tag to a category to further sort the products.

As an example; let's say I had a category displayed to a page using a custom post type called apples. When you landed on that page I wanted to have a filter that would allow the user to sort them by Green Apples. Yellow Apples, Blue Apples and so on.

I might be complicating the issue, but I thought that would be the easiest method. I don't have to time to do further research, so I plan to just to display the multiple categories to the page, and filter through them using tabs. It's a little bit more work for me and doesn't feel very DRY, but it will get the job done until I have the time to figure it out.

Thanks for the suggestion on using global $post.

Hi Christopher,

WordPress does have the ability to use subcategories, this means that in your example you would set Apples as your parent cat and then Yellow, Blue, Red, Green as subcategories only designated to the parent. By doing this you can then use the Categories widget native to WordPress which would display Parent Cats and Sub Cats.

This may well save you a lot of time and I actually found a decent article on the subject.

How to add Categories and Sub Categories to WordPress

Hope this helps Craig