Tutorial: 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 plugin as I will show you in this tutorial.

  1. Open your theme’s Functions.php file
    Along the side of the WordPress dashboard options, hover to ‘Appearance’ then click on ‘Editor’. Once in the Theme Editor, find Theme Functions (functions.php) file in the list on the right of your screen and click on it to open it.
  2. Create a new function for your custom post type
    Enter the following sample of code into your functions.php 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 post type
    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 but 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
  6. 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.

The complete sample code is as follows.

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

If you wish to download the sample code, click here
 
This was intended as a short tutorial but if you have any further questions, comments, feedback or queries please leave them below.

 
 

Comments

Leave a Reply

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