Adding Location to WordPress Posts Using a Hierarchical Custom Taxonomy

Taxonomies in WordPress are basically parameters that add order to pieces of data. WordPress already has tags and categories which are built-in taxonomies (or ways to group information) which are used ot help categorize posts and pages. But going beyond tags and categories can give you extra ways to organize the content on your site in a more logical way.

At WordCamp Toronto, I helped setup custom taxonomies on a site where the site owner wanted to their posts arranged not only by category, but also by location. In this post, I’ll describe how we created a custom hierarchical taxonomy to sort posts by location.

The Problem with Using Standard Tags for Location

Using tags to add location data to the posts, we quickly discovered, posed a few complications.

To start with, there’s a chance that people don’t follow the same conventions when entering locations. For instance, a post tagged “New York City” and one tagged “NYC” will not be on the same location archive page.

The other problem is that it’s unclear how granular the location should be. Some posts could be tagged with a continent (ie. “Europe”) and others could be more specific with a country (“France”), or even a city (“Paris”). In other words, the content needs to be categorized by degrees or gradually.

A hierarchical taxonomy lets you group the content gradually. In this example, content hierarchy lets you have an archive page with all posts under Europe. Another archive page will have all the Europe posts that are also categorized under France. This isn’t normally done with tags, which are a non-hierarchical.

Creating a Hierarchical Location Taxonomy

To create this taxonomy I like to use the Custom Post Type UI plugin. Simply log into your WordPress administrator area, and on the left-hand side menu choose CUSTOM POST TYPES>ADD NEW. Setup a custom taxonomy using the forms (shown below) and click ADVANCED OPTIONS to access hierarchy options.

Create a taxonomy called “location” or “places”. Because this will be a hierarchical taxonomy, in the ADVANCED OPTIONS section change the HIERARCHY dropdown from FALSE to TRUE.

After you’ve created your custom taxonomy, you need to setup location names and how they’re related to one another.

You’ll be able to find these options in your WordPress control panel on the left sidebar in the POSTS menu.

In order to create the location hierarchy, click on the taxonomy name, then add the parent locations, which will be the five continents.

Add the countries, making sure to change the PARENT field to the continent in which it’s located. And finally, add the cities and towns, making sure to change the PARENT field to the country where it’s located.

Adding Locations to the Menu

In order to help users access posts based on the location, it would be good to add them to the menu.

To do this:

1. Choose APPEARANCE>MENU on the dashboard.

2. Select the Places that you want to add to the menu (or SELECT ALL if you want to add all of them), and choose ADD TO MENU.

3. In the menu, child menu items are indented. Make sure that cities are children of countries, and that countries are children of continents.

4. Choose SAVE MENU.

When you create a new post or edit an old one, be sure to add the location on the side menu. And don’t forget to select the parent category as well. (Eg. A post for Paris should have Paris, France and Europe selected.)

Now not only can visitors find your posts based on the category, but also based on the location involved.

Do you have any questions about adding locations to WordPress posts, or anything else to add? Please let us know in the comments!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>