Back to Blog
Lesson 12 of the WordPress Plugin Development: Foundations (PHP & MVC) course
WordPressJune 25, 20263 min read

Introduction to Taxonomies: Organizing WordPress Content

Learn to register custom taxonomies to categorize your WordPress Knowledge Base. Discover how to enable hierarchical features and link them to your CPT.

WordPressPHPTaxonomyCPTPlugin DevelopmentMVCplugin-development

Previously in this course, we explored Configuring CPT Arguments: Mastering Knowledge Article Registration to define our core content structure. Now that we have our "Knowledge Article" CPT, we need a way to group and filter that content. This lesson introduces the taxonomy, the essential WordPress tool for content classification.

What is a Taxonomy?

In WordPress, a taxonomy is a mechanism for grouping posts together. Think of it as a labeling system. By default, WordPress provides two: Categories (hierarchical) and Tags (flat).

When building a Knowledge Base, you often need custom taxonomies—such as "Topic" or "Product Version"—to help users navigate your documentation. By registering a custom taxonomy, you move beyond simple lists and create a structured information architecture that makes your plugin professional and scalable.

Registering a Custom Taxonomy

To register a custom taxonomy, we use the register_taxonomy() function. Just like with Registering Custom Post Types: A WordPress Developer’s Guide, we hook this into the init action.

Here is how we implement it within our MVC structure. In your PluginCore class (or a dedicated TaxonomyManager class), add the following:

PHP
public function register_knowledge_taxonomies() {
    $labels = [
        'name'              => 'Topics',
        'singular_name'     => 'Topic',
        'search_items'      => 'Search Topics',
        'all_items'         => 'All Topics',
        'edit_item'         => 'Edit Topic',
        'update_item'       => 'Update Topic',
        'add_new_item'      => 'Add New Topic',
        'new_item_name'     => 'New Topic Name',
        'menu_name'         => 'Topics',
    ];

    $args = [
        'hierarchical'      => true, #6A9955">// Enables parent/child relationships
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => ['slug' => 'kb-topic'],
    ];

    register_taxonomy('kb_topic', ['knowledge_article'], $args);
}

Breaking Down the Arguments

  • hierarchical: When set to true, your taxonomy behaves like categories (you can have parent and child topics). Setting it to false makes it behave like tags.
  • show_admin_column: This is a pro-tip. Setting this to true automatically adds a column to your Knowledge Article list table in the admin, allowing you to see and filter by topic at a glance.
  • The second parameter (['knowledge_article']): This is crucial. It explicitly associates this taxonomy with our custom post type. If you leave this array empty, the taxonomy won't be attached to any post type by default.

Hands-on Exercise

  1. Open your plugin's main initialization file.
  2. Add an add_action('init', [$this, 'register_knowledge_taxonomies']); line to your constructor.
  3. Implement the method shown above.
  4. Navigate to your WordPress dashboard, refresh, and verify that "Topics" appears under your "Knowledge Articles" menu.
  5. Create two topics, one as a child of the other, to verify the hierarchical functionality.

Common Pitfalls

  • Forgetting to Flush Permalinks: If you change the rewrite slug, your taxonomy archives might return 404 errors. Simply visit Settings > Permalinks and click "Save Changes" to flush the rewrite rules.
  • Taxonomy Name Length: Keep your taxonomy slug short and avoid using reserved WordPress names (like category or post_tag). kb_topic is safe and descriptive.
  • Missing Post Type Association: If your taxonomy doesn't show up on the edit screen, double-check that the second argument of register_taxonomy includes your CPT slug exactly as defined in your registration function.

Recap

We’ve successfully introduced a classification layer to our Knowledge Base. By utilizing a hierarchical taxonomy, we’ve enabled nested content organization, which is vital for documentation plugins. You now know how to define labels, enable administrative visibility, and link these taxonomies to your CPT.

Up next: We will begin the process of capturing custom data for our articles by Designing Meta-Boxes.

Similar Posts