Using a Custom Taxonomy to Add a Location Drop Down
Recently in the support forums, one of our members wanted to add US states to his Directory listings and make his listings searchable by state.
While there are plugins that can do this, we chose a more interesting path that takes advantage of our plugins.
In today’s Weekend WordPress Project I’m going to show you how to use a custom taxonomy to add a location drop down to your site.
Plugins You Will Need for This Project
These are the plugins you’ll need for this project:
- CustomPress, or another plugin that will allow you to create custom taxonomies.
- Directory, or any plugin that creates content that should be sorted by state or location.
- An advanced search plugin that can search custom post types. I chose Advanced Custom Post Search or Ultimate WP Query Search Filter.
1. Create New Taxonomy for Your States/Locations
In CustomPress, navigate to the Taxonomies tab, and select Add New.
Assign your taxonomy’s system name, which, like a slug, should be all lowercase letters. I’m going to call my “state.”
Assign your taxonomy to the relevant post types. For my example here, I added the “states” taxonomy to both Directory Listing and Classified custom post types.
Change any of the labels referring to your taxonomy in the box called “Labels.”
Set the following settings to “true” if they aren’t already:
- Show Admin Column
- Show UI
- Show in Nav Menus
And don’t forget to hit “Save.”
2. Add Your States or Locations
Next, navigate to Listings (or whichever plugin you’re using) > States (or your label) and add each location, just like adding a list of categories for standard posts.
Check to make sure everything is working as planned by going to Listings > Add New, and ensure you’ve got a shiny new location dropdown on the edit post screen.
If everything looks cherry, move on to the next step.
3. Enable Search by State/Location
The screenshots below are using Advanced Custom Post Search, your search plugin may look a bit different behind the scenes.
First, select the post type you’d like the search to apply to. In this case, I chose the Directory custom post type.
Next, select which taxonomies you would like the search to use. Here, I selected listing category and state.
Customize the other settings to your liking, and hit “Save.”
4. Add to Your Site
Advanced Custom Search comes with both a handy widget and a shortcode, which allow you to place the search tool wherever you’d like. I used Custom Sidebars to create a sidebar for my Listings pages with a list of the most recent Listings underneath the Advanced Custom Search widget.
That’s it! Now if you’d like to make your Listings or Classified (or more!) custom posts available and searchable by state, you’ve got all the tools you need.
What kinds of cool stuff have you done with CustomPress and custom post types? Let us know in the comments below.Tags: