Skip to main content
Dublin Library

The Publishing Project

WordPress Custom Post Type for Glossary Project

 

I've been playing with the idea of creating a technical glossary for referring to technical terms on my posts. After a lot of research I came back to a simple tool: WordPress custom post types (CPT). The idea is as follows: 1. Create a custom post type plugin for glossary entries * Create the post type * Change the labels as appropriate * Ensure that it will appear in the REST API and use Gutenberg if desired 2. Create a corresponding taxonomy * Change the labels as appropriate * Ensure that it will appear in the REST API 3. Explore how to incorporate the CPT and taxonomy into an existing theme * Ensure that it will appear on the home page if needed or desired * Create additional templates to work with the custom post type * Flush the cache on plugin activation (and only on activation) ## Create Glossary Entry Custom Post Type Creating the Custom Post Type is not hard, it's just somewhat cumbersome. I first created an array of names and internationalized values for the different labels that I want to change from the default. I put the array in a separate variable to make the code easier to read. ```php __( 'Glossary Entries', RWPDOMAIN ), 'singular_name' => __( 'Glossary Entry', RWPDOMAIN ), 'featured_image' => __( 'Entry Image', RWPDOMAIN ), 'set_featured_image' => __( 'Set Entry Image', RWPDOMAIN ), 'remove_featured_image' => __( 'Remove Entry Image', RWPDOMAIN ), 'use_featured_image' => __( 'Use Glosary Entry Image', RWPDOMAIN ), 'archives' => __( 'Glossary', RWPDOMAIN ), 'add_new' => __( 'Add Glosary Entry', RWPDOMAIN ), 'add_new_item' => __( 'Add Glosary Entry', RWPDOMAIN ), ); ``` The second variable holds an array of the arguments for the CPT that we will use when registering the post The two important attributes in the array are: * `supports` tells WordPress what areas of the editor are available in this Custom Post Type * `show_in_rest` makes the CPT available through the REST API and activates the Gutenberg editor for this CPT when set to true ```php $labels, 'public' => true, 'has_archive' => 'glossary', 'rewrite' => array( 'has_front' => true ), 'menu_icon' => 'dashicons-book', 'supports' => array( 'title', 'editor', 'thumbnail', ), // Line below makes CPT available in rest // Line below makes CPT available to //Gutenberg/Block editor 'show_in_rest' => true, ); ``` We need to run the `register_post_type` function. It takes two parameters: the name of the CPT and an array of arguments we defined in the `$args` variable. ```php 'Genres', 'singular_name' => 'Genre', 'search_items' => 'Search Genres', 'edit_item' => 'Edit Genre', 'add_new_item' => 'Add New Genre' ); ``` Next, we define an array of arguments. The biggest difference between this array and the one we used to define the book CPT is that we are making the taxonomy hierarchical. This will allow to create parent-child relationships like **non-fiction -> technology** or **fiction -> science fiction** ```php $tax_labels, 'show_in_rest' => true, 'hierarchical' => true, 'query_var' => true, 'rewrite' => array( 'has_front' => true ), 'supports' => array( 'title', 'editor', 'thumbnail' ), ); ``` The `register_taxonomy` function takes three parameters: the name of the taxonomy, the name of the post type it's associated with and an array of arguments for the taxonomy (that we defined in the `$args` variable) ```php array( 'genre', ), ``` The array contains all the custom taxonomies that we want to use with the CPT. For books, we could also add authors and publishers taxonomies.

Edit on Github