Create a custom taxonomy in WordPress

Custom taxonomies can be used in WordPress to organise posts and custom post types similarly to how posts are organised into categories. In fact categories are a type of taxonomy to organise posts. An example of a custom taxonomy may be a ‘genre’ taxonomy to organise review posts, or a ‘location’ taxonomy to organise listings by area. Similarly to creating a custom post type, with a few lines of PHP code it is very easy to create a simple custom taxonomy without the use of a third party plugin as I will show you in this tutorial.

1. Open your theme’s functions.php file or your custom plugin’s core file

Access your theme or plugin files via SFTP or cPanel file manager. This is the preferable way to edit these files although you can through wp-admin also. If editing the theme’s functions.php file within your wp-admin dashboard. Move your cursor along the side of the WordPress dashboard options, and hover to ‘Appearance’ then click on ‘Editor’. Once in the Theme Editor, find the Theme Functions (functions.php) file in the list on the right of your screen and click on it to open it. If adding the custom taxonomy through a custom plugin hover over to ‘Plugins’ then select ‘Plugin Editor’. From the dropdown on the right hand side, select your plugin then click ‘Select’.

2. Create a new function for your custom post type

Enter the following sample of code into your file, changing the name of the function (create_custom_taxonomy) to any other name if you wish. But do not yet update the file.

function create_new_taxonomy() {
}

3. Register the new taxonomy

Enter the following code sample (underlined) between the { } brackets:

function create_new_taxonomy() {
    register_taxonomy( ‘genre’,
        ‘post’,
            array(
                ‘label’ => __( ‘Genre’ ),
                ‘rewrite’ => array ( ‘slug’ => ‘genre’ ),
                ‘has_archive’ => true,
                ‘heirarchial’ => true )
         );
}

As an example, here I am creating a ‘genre’ taxonomy for regular posts. For custom post types, change the second parameter (‘post’) to the slug for another post type. Although the taxonomy is set for genre, this could easily be for Location, Author and Format or anything you can imagine. Change the ‘genre’ parameter according to the taxonomy you wish to create. If you wish to use the taxomony for a custom post type, change ‘post‘ to the slug for the custom post type. For example, if you want to use the taxonomy for any Review posts where the slug for the review post type is slug, change the ‘post’ parameter to ‘review’. The basic code is the same regardless. You can change the following parameters within the array:

a. ‘label’ – this is what the custom taxonomy label is displayed as in the Dashboard left-hand side posts menu.
b. ‘rewrite’ – the name for the ‘slug’ for the taxonomy (used in the url for the archive page or in the archive template name).
c. ‘heirarchial’ – either true or false to determine whether taxonomy terms can have children and parents, like the relationship between categories and subcategories.
d. ‘has_archive’ – set to true or false to determine whether there is an archive page for terms in this taxonomy.

4. Include add_action command for the function

Enter the following code sample (underlined) after the closing } bracket for the function:

function create_new_taxonomy() {
    register_taxonomy( ‘genre’,
        ‘post’,
            array(
                ‘label’ => __( ‘Genre’ ),
                ‘rewrite’ => array ( ‘slug’ => ‘genre’ ),
                ‘has_archive’ => true,
                ‘heirarchial’ => true )
         );
}
add_action( ‘init’, ‘create_new_taxonomy’ );

If you changed the name of the function earlier, you should use the same function name in the add_action command.

5. Click ‘Update File’ or upload/save changed file to the server

Your custom taxonomy should now display under the posts submenu ‘Posts’ on the left-hand side menu in your Dashboard view
If you have registered the taxonomy for use in a custom post type, it will be under the menu for that post type instead.

This was intended as a short tutorial but if you have any further questions, comments, feedback or queries please leave them as a comment below.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.