Taxonomies
Intro
Inside Launchpad's /inc/taxonomies.php file there are examples for three kinds of custom taxonomies:
- Simple: standard WordPress taxonomy
- Select: metabox displays as a select box
- PostType: terms are generated by post data, admin page is hidden
All taxonomies should be initialized and managed through the /inc/taxonomies.php file.
Taxonomy Slugs
The slug you use for a custom taxonomy will depend on whether or not the taxonomy is shared between post types, and whether or not it shares a name with a post type.
Single-Use Taxonomies
For taxonomies assigned to only one post type, the slug should be {posttype}-{taxonomy-name}. So in the event of a taxonomy called "Type" used on a post type called "Books," the slug would be books-type.
Hyphenating Taxonomy Slugs
When providing a slug for taxonomies, please use hyphens instead of underscores between words so as to create more commonplace URLs.
Shared Taxonomies
For taxonomies shared across multiple post types, the slug should just be {taxonomy-name}. That means for a taxonomy called "Issues" shared by three post types called "Blogs," "News" and "Reports," the slug would be issues.
Custom Post Type & Taxonomy Slugs
Occasionally, a shared custom taxonomy will have the same name as a custom post type, e.g. "Programs." If the slugs are the same, in this example programs, it will break several pieces of built-in WordPress functionality, including archive filters.
In this situation, please default to a plural slug for the custom post type, and a singular slug for the taxonomy. So that would be programs for the post type and program for the taxonomy.
Simple Taxonomies
These are the most basic of custom taxonomies, and only require a register_taxonomy() function to work.
Hierarchical
Simple taxonomies are marked as non-hierarchical by default — make 'hierarchical' => true, to enable.
Metabox Callback
Non-hierarchical taxnomies display as a tag cloud by default, with the ability to add new terms on the fly. So as to avoid term bloat, we can set 'meta_box_cb' => 'post_categories_meta_box', and simple taxonomies will display using the hierarchical taxonomy metabox.
Select Taxonomies
Select taxonomies initialize much the same as simple taxonomies, but are displayed as <select> elements with the use of custom callback and post save functions. This enforces assigning only one term per post at a time.
Metabox Callback
In order to create a <select> element for the taxonomy, we must provide a custom callback function.
Post Save Action
Since we're creating a custom element for the taxonomy information, we also need to add an action on post save for WordPress to save the assigned terms.
Saving on Multiple Post Types
The function above is written to fire when a particular post type is saved (i.e. events), instead of firing on every post type. This is simply safer and more efficient. In the event your taxonomy belongs to multiple post types, you can add another add_action() for each additional post type, replacing the first parameter with the appropriate action slug.
PostType Taxonomies
These taxonomies have their terms generated by the posts of a specified post type, requiring four total functions to register, display, save and sync terms.
The main purpose of these taxonomies is to create direct relationships between posts of different post types. In the example code that follows, we are setting up the taxonomy "Program" to appear on "Stories" posts, but instead of managing the terms like normal, the terms will be automatically created, updated and deleted by managing posts in "Programs" post type.
Hiding the Taxonomy UI
In order to make sure that PostType taxonomy terms stay in sync with their related post type, we set 'public' => false, and 'show_in_menu' => false, to hide the associated WP admin pages. However, since 'publicly_queryable', 'show_in_nav_menus' and 'show_ui' inherit their values from the 'public' setting, we must explicitly set them to true in order for the tax to work as expected.
Hidden, but not Gone
If you do need to access a PostType taxonomy's admin page while building, you still can, its link just won't show up in the WordPress sidebar nav.
To access the tax admin page, you can visit https://{site-address}/wp-admin/edit-tags.php?taxonomy={taxonomy-slug}.
Metabox Callback
This callback will create a non-editable list of terms as checkboxes, allowing for multiple terms to be assigned per post. You can also easily adapt the Select Tax Callback to force a single term per post.
Post Save Action
As with any taxonomy that utilizes a custom metabox, we must include a custom post save action.
Syncing Posts to Taxonomy Terms
This function is the magic behind the PostType taxonomy. It fires on post save for the associate post type (instead of the post type the taxonomy is assigned to), and compares the title of the post with the current list of taxonomy terms.
In the PostType examples thus far, we've been working with a taxonomy called "Programs," which is assigned to the "Stories" post type, and gets its terms from the "Programs" post type (aka its associated post type). In this case, the following function will fire when a "Programs" post is created, edited or deleted, and will create, edit or delete terms in the "Programs" taxonomy as appropriate.
Watching Post Title, not Slug
This function is built to watch the post title only, and will update both the corresponding term name and slug when changed. While it can sometimes be helpful to give pages with long titles a shortened URL, we've elected to keep tax term titles and slugs in sync to ensure functionality site-wide, and especially on archives.