Using Custom Taxonomies and Custom Fields in WordPress Development

This course covers the key points of two of WordPress’s most powerful APIs for defining custom post data: custom fields (also called post meta), and custom taxonomies. The course introduces each tool, and then—since some problems can be addressed by either tool—covers practical guidelines for when to use custom fields and when to use custom taxonomies.

1. Creating WordPress Custom Taxonomies

How and Why to Create WordPress Custom Taxonomies

Key Point: Flat and Hierarchical Taxonomies

A WordPress taxonomy is a way of organizing WordPress posts.

A WordPress taxonomy is a way of organizing WordPress posts. Taxonomies come in two flavors: flat and hierarchical. Elements of a hierarchical taxonomy can be members of one another: for example, “Romantic Comedies” being a member of “Comedies” inside a “Genre” taxonomy; flat taxonomies don’t have this feature.

Tags are WordPress’s default flat taxonomy, and Categories are WordPress’s hierarchical taxonomy—but you can also register custom taxonomies of either type.

Key Point: Basic Use of register_taxonomy()

To create custom taxonomies by hand, you use register_taxonomy(), which you hook into WordPress’s init action hook.

A simple, but complete, use of register_taxonomy() looks as follows:

add_action('init', 'our_awesome_registration_of_taxonomies');
function our_awesome_registration_of_taxonomies() {
    $args = array( 
        'hierarchical' => true,
        'label' => 'Awesome Taxonomy',
    register_taxonomy( 'awesome_taxonomy', array( 'post', 'page', 'awesome' ), $args );

This creates a hierarchical custom taxonomy whose display name is “Awesome Taxonomy,” whose slug is awesome_taxonomy, and which applies to the post, page, and awesome post types.

2. Creating WordPress Custom Fields

Everything You Should Know about WordPress Custom Fields

Key Point: The Role of Custom Fields

Custom fields, also called post meta, are simple stores for data about a WordPress post.

Custom fields, also called post metadata or post meta, are simple stores for data about a WordPress post. Unlike custom taxonomies, they don’t help organize multiple WordPress posts. Rather, they are post-level data about individual posts themselves.

Custom fields have a huge number of uses. For example, an e-commerce solution like WooCommerce uses custom fields to give its “Product” posts most of their important data—like price, shipping weight, quantity in stock, and so on. Similarly, an SEO plugin like Yoast stores the post-level data you define—such as a focus keyword—as custom fields.

In each case, these pieces of post metadata enrich individual posts in crucial ways, without organizing or grouping posts like a taxonomy would.

Key Point: Functions for Working with Post Metadata

To display and manage stored post metadata, you use a set of PHP functions. Perhaps the most important is get_post_meta(), which looks as follows:

get_post_meta( $post_id, $key, $single );

A sample use of this function, inside the Loop, to get a piece of post meta called mood as a single string, would look as follows:

$post_mood = get_post_meta( get_the_ID(), 'mood', true );

You could then display this string wherever you like with echo $post_mood;.

add_post_meta(), update_post_meta(), and delete_post_meta() are also important functions for working programmatically with custom fields inside the PHP logic of a plugin or theme.

Key Point: Creating Custom Metaboxes

Custom metaboxes let users easily modify values for specific custom fields in the WordPress post editor.

Custom metaboxes are what let you or other users easily define or modify values for specific custom fields inside the WordPress post editor.

WordPress’s default interface for creating metaboxes is complicated to use, and a number of good alternative solutions exist. We recommend two developer tools, both on GitHub: Human Made’s Custom Meta Boxes project, or CMB2 by WebDev Studios.

A number of less technical solutions exist as well, such as the Advanced Custom Fields plugin. These are less feature rich than the developer solutions, but simpler to use in general.

3. Knowing When to use Custom Fields and When to Use Custom Taxonomies

Organizing Your WordPress Data: Understanding Custom Taxonomies vs Custom Fields

Key Point: Custom Fields are Best for Unique, Potentially Sortable Data

The article uses the example of a movie listing and review site, with a large number of “Movie” posts. Should a given movie’s “Release Date” be a custom taxonomy or a custom field?

The article concludes that the flexibility of a custom field makes it the best choice. You might just create a hierarchical taxonomy like 1990s > 1995—but this is very stiff and inflexible. What if you someday decide that you want to specify the month of release as well? That leads to a very strung-out taxonomy, with hundreds of entries like 1990s > 1995 > October.

A custom field, by contrast, can be unique and sortable. Using a custom field, you can specify that a particular movie was released on Thursday, July 20, 1995—a unique date that no other movie shares. What’s more, you can later sort by that custom field value to retrieve all movies released on a Thursday, or on the 20th of the month.

The same logic suggests custom fields for other types of data that can take a large number of values, and that are best used as a method to sort through posts rather than as a way to organize them. “Price” is another excellent example, and is always handled as a custom field rather than a custom taxonomy. Similarly, a video game website that allowed ratings like “9.8/10” should store those ratings as a custom field—rather than as a taxonomy of dozens of different decimal ratings—and allow users to sort posts by rating. This sorting functionality will have to be written by hand, since there is no default way to sort posts by a given custom field value.

Key Point: Custom Taxonomies are Best for Non-Unique, Organizing Data

Taking again the example of a movie review site: if the site’s ratings system is instead “Thumbs Up/Thumbs Down,” should this be handled with a custom taxonomy or a custom field?

This is best handled as a custom taxonomy. Because there are a small number of possible values (two) into which many posts are organized—rather than, as with “Release Date,” many potentially unique values which posts might take on—a taxonomy is appropriate.

Furthermore, the taxonomy should be hierarchical: even though there will be no “subcategories,” the taxonomy more closely resembles two well-defined “buckets” or “categories” that posts go into than it resembles an arbitrarily large set of “tags” that might help organize those posts.

Using this ratings taxonomy will make it easy for the site to have, for example, a page that lists all “Thumbs Down” movies, since WordPress can make taxonomy archive pages by default. (When you view all posts of a given category on a WordPress blog, you’re viewing an archive page for the Category taxonomy.) This removes the need to program any particular method of “sorting” the two values, which would need to be done by hand if this were a custom field.

Now You Know Custom Taxonomies and Custom Fields!

Custom taxonomies and custom fields are two of the primary technologies that make WordPress a flexible and powerful technical system. After this introduction, you should have a good grounding in the fundamentals of both tools, as well as a good intuitive sense of which situations call for one and which call for the other.

Thanks for reading, and as always we’d love to hear your questions and comments!

Image credit: alborzshawn

2 Responses



Add a comment