Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Adding a taxonomy

Koen edited this page Sep 12, 2019 · 6 revisions

Taxonomies are used to add categories or tags to post types. We're using TaxonomyAbstract with sensible defaults, which is extended per taxonomy.

Creating a taxonomy

  • Create a class in Grrr\Taxonomies, extending TaxonomyAbstract
  • Call the register function in functions.php

An example for a taxonomy class could be:

namespace Grrr\Taxonomies;

class ExampleType extends TaxonomyAbstract {

    protected $_taxonomy           = 'example-type';
    protected $_slug               = 'type';
    protected $_name               = 'Example Types';
    protected $_singular_name      = 'Example Type';

    protected $_args = [
        'rewrite' => [
            'slug' => '<post-type-slug>/type',
        ],
    ];

}

For all available arguments of the underlying register_taxonomy used, view the WordPress codex or code reference.

Binding it to a post type

There are two ways to bind a taxonomy to a post type:

  • Specify the post type during taxonomy registration. It's the second parameter of the register_taxonomy method, and it can either be an array or a single post type.
  • Add it to the taxonomies array while registering the post type.

We're using the second method, since that show in the post type itself which taxonomies it's using.

Naming caveats

  • The registered name cannot be longer than 32 characters