Creating Custom Taxonomies in WordPress

Contents

Creating Custom Taxonomies in WordPress

Taxonomies are a fundamental part of WordPress’s content organization system. Out-of-the-box, WordPress offers two built-in taxonomies: categories and tags. But for many custom post types or specific projects, you’ll want to define your own taxonomies to better categorize and filter content. This article delves into the why, what, and how of creating custom taxonomies in WordPress, with extensive code samples, best practices, and resources.

1. Why Use Custom Taxonomies

  • Granular organization: Group related content beyond generic categories or tags.
  • Improved UX: Provide users with custom filters on archive pages.
  • SEO benefits: Generate dedicated archive pages for taxonomy terms.
  • Content modeling: Align your site structure with business or editorial requirements.

2. Default WordPress Taxonomies

Taxonomy Description Hierarchical
category Organizes posts hierarchically. Yes
post_tag Tags for tagging posts freely. No
link_category Categorizes links (deprecated). Yes
post_format Format types for posts (video, gallery, etc.). Yes

3. The register_taxonomy() Function

The core function to create a custom taxonomy is register_taxonomy(). You generally hook it to the init action in your theme’s functions.php or a custom plugin.

add_action(init, myplugin_register_taxonomy)
function myplugin_register_taxonomy() {
  labels = array(
    name              => Genres,
    singular_name     => Genre,
    search_items      => Search Genres,
    all_items         => All Genres,
    parent_item       => Parent Genre,
    parent_item_colon => Parent Genre:,
    edit_item         => Edit Genre,
    update_item       => Update Genre,
    add_new_item      => Add New Genre,
    new_item_name     => New Genre Name,
    menu_name         => Genres,
  )

  args = array(
    labels            => labels,
    hierarchical      => true,
    public            => true,
    show_ui           => true,
    show_in_rest      => true,
    rewrite           => array(slug => genre),
    capabilities      => array(
      manage_terms  => manage_genres,
      edit_terms    => edit_genres,
      delete_terms  => delete_genres,
      assign_terms  => assign_genres
    ),
  )

  register_taxonomy(genre, array(post, book), args)
}

Key Arguments Explained

Argument Description
labels Array of interface labels.
hierarchical True for category-like false for tag-like.
public Controls visibility on front-end.
show_in_rest Enable Gutenberg and REST API support.
rewrite Customize URL slug and permalinks.
capabilities Define granular permissions.

4. Assigning Taxonomies to Post Types

When calling register_taxonomy(), the second parameter determines which post types will support this taxonomy. You can pass a single post type string or an array:

  • register_taxonomy(topic, post, args)
  • register_taxonomy(skill, array(post, course), args)

5. Displaying Taxonomy Terms in Templates

To display terms associated with a post:

ltphp
echo get_the_term_list( get_the_ID(), genre, ltulgtltligt, lt/ligtltligt, lt/ligtlt/ulgt )
gt

Or use wp_list_categories() for archive sidebars:

ltphp
wp_list_categories( array(
  taxonomy     => genre,
  orderby      => name,
  show_count   => true,
  title_li     => lth4gtGenreslt/h4gt
) )
gt

6. Customizing Taxonomy Template Hierarchy

WordPress will look for:

  1. taxonomy-lttaxonomygt-lttermgt.php
  2. taxonomy-lttaxonomygt.php
  3. taxonomy.php
  4. archive.php

Create dedicated templates to control the layout and queries for your custom taxonomy archives.

7. Advanced Features

  • Taxonomy Metadata: Store term-specific data with functions like add_term_meta(). More at developer.wordpress.org.
  • Custom Rewrite Rules: Use rewrite => array(slug=>genre,with_front=>false) and flush_rewrite_rules() when activating your plugin.
  • Roles Capabilities: Fine-tune who can manage, edit, or assign taxonomy terms.
  • REST API Gutenberg: Setting show_in_rest => true exposes taxonomy endpoints under /wp/v2/genre.

8. Managing the Admin UI

By default, WordPress will add a meta box on post edit screens. You can reposition or customize it using hooks like add_meta_boxes or remove it entirely:

remove_meta_box(genrediv, post, side)

To add custom columns to the taxonomy list table:

add_filter(manage_edit-genre_columns, set_custom_genre_columns)
function set_custom_genre_columns(columns) {
  columns[term_image] = Image
  return columns
}

add_filter(manage_genre_custom_column, custom_genre_column, 10, 3)
function custom_genre_column(out, column, term_id) {
  if (column == term_image) {
    image = get_term_meta(term_id, genre_image, true)
    if (image) {
      out = ltimg src= . esc_url(image) .  style=max-width:50px height:auto /gt
    }
  }
  return out
}

9. Best Practices Tips

  • Use unique slugs for taxonomy names to avoid conflicts.
  • Decide hierarchical vs. non-hierarchical based on content needs.
  • Always sanitize and escape taxonomy metadata for security.
  • Consider performance: very large term sets may need caching strategies.
  • Leverage plugins like Custom Post Type UI for quick setup, then refine in code.

10. Further Reading Resources

By following these guidelines and examples, you’ll be able to create robust, tailored taxonomies that enhance both the management and presentation of your WordPress content.



Acepto donaciones de BAT's mediante el navegador Brave 🙂



Leave a Reply

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