What Is a Taxonomy Term in WordPress?
A taxonomy term in WordPress is an individual item within a taxonomy, which is a system for categorizing content. Taxonomy terms help group posts, pages, or custom post types based on shared attributes, improving content organization and navigation.
Default Taxonomies and Their Terms
WordPress includes two built-in taxonomies: categories and tags. Each taxonomy contains terms for classification.
Categories
Categories are hierarchical, allowing parent-child relationships. They help organize content broadly. For example, a news website might have categories like:
- World News
- Local News
- Entertainment
- Technology
Each category can have subcategories. A “Technology” category might have child terms like “Gadgets” or “AI.” When no category is assigned, WordPress automatically applies the “Uncategorized” category.
Tags
Tags are non-hierarchical and act as keywords describing content details. A blog post about a mystery novel could have tags like:
- Mystery
- Fiction
- Stephen King
- Agatha Christie
Unlike categories, tags do not support parent-child relationships.
Custom Taxonomies and Custom Terms
WordPress allows developers to create custom taxonomies tailored to specific needs. A recipe website could use:
- Meal Type (Breakfast, Lunch, Dinner)
- Cuisine (Italian, Japanese, Mexican)
To register a custom taxonomy, use the register_taxonomy function in a theme’s functions.php file or a custom plugin.
php
function register_custom_taxonomy() {
register_taxonomy(
'recipe-cuisine',
'post',
array(
'labels' => array(
'name' => 'Cuisine',
'singular_name' => 'Cuisine',
),
'hierarchical' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'cuisine' ),
)
);
}
add_action('init', 'register_custom_taxonomy');
This code registers a hierarchical taxonomy called “Cuisine” to classify posts.
Managing and Displaying Taxonomy Terms
Assigning Terms
WordPress enables users to assign taxonomy terms via the post editor or programmatically. To set a term via code:
php
wp_set_object_terms( $post_id, 'Japanese', 'recipe-cuisine' );
Querying and Displaying Terms
To retrieve taxonomy terms of a post:
php
$get_terms = get_the_terms($post->ID, 'recipe-cuisine');
if ($get_terms && !is_wp_error($get_terms)) {
foreach ($get_terms as $term) {
echo $term->name;
}
}
To display all terms of a taxonomy:
php
$terms = get_terms(array(
'taxonomy' => 'recipe-cuisine',
'hide_empty' => false,
));
foreach ($terms as $term) {
echo '<a href="' . get_term_link($term) . '">' . $term->name . '</a>';
}
Custom Taxonomy Templates
WordPress automatically generates archive pages for taxonomy terms. Custom templates such as taxonomy-recipe-cuisine.php allow for specific display modifications.
```php
get_header();
?>
<?php
if (have_posts()) :
while (have_posts()) : the_post();
the_title('', '');
the_excerpt();
endwhile;
endif;
get_footer();
```
Setting Default Terms
To ensure content organization, default terms can be set when saving a post.
php
function set_default_terms($post_id, $post) {
if ('publish' === $post->post_status) {
$default_terms = array('recipe-cuisine' => array('General'));
foreach ($default_terms as $taxonomy => $terms) {
if (empty(wp_get_post_terms($post_id, $taxonomy))) {
wp_set_object_terms($post_id, $terms, $taxonomy);
}
}
}
}
add_action('save_post', 'set_default_terms', 100, 2);
This ensures that posts in the “recipe-cuisine” taxonomy default to “General” if no term is selected.
Common Issues with Taxonomy Terms
Duplicate Terms
WordPress allows duplicate term names under different taxonomies but not within the same taxonomy. This can cause confusion when managing terms.
Nested Taxonomies and URL Structures
Hierarchical taxonomies can cause unexpected permalink structures. For example:
example.com/cuisine/italian vs. example.com/cuisine/pasta/italian
Customizing permalinks via rewrite_rules_array can improve term URL handling.
Programmatic Term Updates
When updating term names or slugs programmatically, associated posts must also be updated. Use wp_update_term():
php
wp_update_term($term_id, 'recipe-cuisine', array(
'name' => 'Modern Italian',
'slug' => 'modern-italian',
));
WordPress Core and Historical Context
WordPress introduced custom taxonomies in version 2.3, allowing greater content classification flexibility. Since then, taxonomy handling has been extended in API capabilities, default behavior changes, and accessibility improvements. Categories and tags remain foundational to WordPress’s content organization system.