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

WordPress

Kevin Korte
Kevin Korte
28,149 Points

Chained Select dropdown nav using custom taxonomies

My head is hurting from this, and I'm afraid this may be more advance that I'm capable of doing right now. But here it goes.

I have set up 4 custom taxonomies for my custom post type in wordpress. No problem.

I even set up my select menus using get_categories where each select menu populates with all of the options for that taxonomy. That list updates as I add more taxonomy values. Again, no problems there.

What I would like, is that each post, will have one option from each of the 4 taxonomies.

The 4 taxonomies are:

1) Type (i.e. ATV, Motorcycle, Snowmobile, Watercraft) 2)Year (i.e. 2012,2011,2010) 3)Make (i.e. Suzuki, Honda, Yamaha) 4)Model (i.e. RM250, YZ125, CRF450)

The goal is you chose your type option, say Motorcycle, and than the year populates with all of the year taxonomy values that also have a type of motorcycle. You than select your year, and the Make populates with all of the makes that also have that year and type, and than you select your make, and the model populates with all the models that also have that make, year, and type.

And then after you select your model, it loads that post that match all 4 taxonomies. There will only be one post that can possibly match all 4.

My head has been spinning with this problem I'm dizzy. Somebody please point me in the right direction.

Thanks all!

2 Answers

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi Kevin Korte,

This is actually a fairly advanced technique, but I'm sure it's possible. :)

Doing this will require several database queries and some JavaScript. Basically, each time a user selects a menu item, you'll need to fire off some JavaScript that sends the selection to the server. The selection should be used in a database query that then populates the subsequent select menu based on the relationships. You can do this asynchronously (where each menu populates without a page refresh) or with a page refresh, being sure to maintain the previous selections.

If you haven't taken our Database Foundations course, I recommend you start there and get some fundamental knowledge in.

Kevin Korte
Kevin Korte
28,149 Points

Hi Nick,

Thanks for the answer. That's along the lines I was thinking; just wanted to make sure I was climbing the right mountain so to speak before I really started on this project.

I've fortunately found some WP plugins that offer similar features. I've been dissecting their code to see how they did it. I think I've done all buy one lesson the Database with Andrew, I'll certainly go back through all of that as I get ready here.

I think this will be the hardest thing I'll have to build for the site, but at least since then I've found it certainly is possible.

Thanks!