In this post, I’ll outline how to create a taxonomy dropdown in WordPress that lets the user choose an option, then reloads the page to only show entries that match the taxonomy term they chose. This is extremely useful for filtering a list of results to show only what your visitor is interesting in seeing.
Let’s say we have a custom post type called ‘tool’, and we’ve created a custom taxonomy to organize our tools called “tools”. Our “tools” taxonomy has terms such as “hammer”, “nails”, “saw”, etc.
To customize the code below to work for your purposes:
- Change ‘tool’ to whatever post type you’re using -- this could be ‘post’ or ‘page’ (which are both built in to WordPress) or any custom post type
- Change ‘tools’ to whatever taxonomy you’re using -- this could be ‘category’ or ‘tag’ (which are both built in to WordPress) or any custom taxonomy
- Change the “km_” function prefix to something unique.
Put this code in functions.php:
Then put this code in your page template:
Happy filtering 🙂