Create Custom Taxonomies in Wordpress

Taxonomy is like a category for your custom post type. By default wordpress comes with builtin taxonomy "Category" for "Post". In this post we will register a custom taxonomy "Star" for our custom post type "Movies" we created in previous wordpress post.

Create Custom Taxonomies in Wordpress

Custom Taxonomies

Wordpress allows you to register custom taxonomies for your custom post types.Custom taxonomy in wordpress is like a group for your specific posts. Let's say we have a custom post type "products" so we can create different custom taxonomies like "Mobiles", "Computers", "Laptops" etc. In this post we will register a custom taxonomy "Star" for our custom post type "Movies".
The wordpress function used to register a custom taxonomy is
register_taxonomy( $slug, $post_types, $args );
The first Parameter is slug of taxonomy, second parameter is array of post types this taxonomy will be linked to and third parameter is array of arguments for this taxonomy.We will first create a file custom_taxonomies.php which will contain the code to register this new taxonomy and we include this file in our functions.php.

custom_taxonomies.php

<?php
add_action("init", "theme_taxonomies");
function theme_taxonomies() {
    // --- Labels Array --- //

    // General name for the taxonomy, usually plural
    $labels["name"]         = __( "Stars", "textdomain" );

    // Name for one object of this taxonomy
    $labels["singular_name"]= __( "Star", "textdomain" );

    // Taxonomy name in menu
    $labels["menu_name"]    = __( "Stars", "reelio" );

    // Label to signify all items in a submenu link
    $labels["all_items"]    = __( "All Stars", "textdomain" );

    // Label for adding a new term item button on list screen
    $labels["add_new_item"] = __( "Add New Star", "textdomain" );

    // Label for link to add a new term item
    $labels["new_item_name"]= __( "New Star", "textdomain" );

    // Label for link to edit term item
    $labels["edit_item"]    = __( "Edit Star", "textdomain" );

    // Label for link to view term
    $labels["view_item"]    = __( "View Star", "textdomain" );

    // Label for link to update a term item
    $labels["update_item"]  = __( "Update Star", "textdomain" );

    // Label for search item button
    $labels["search_items"] = __( "Search Star", "textdomain");

    // Label for text when no term is found
    $labels["not_found"]    = __( "No Star Found", "textdomain");

    // The labels array we prepare above
    $args["labels"]             = $labels;
    // Whether a taxonomy is intended for use publicly
    $args["public"]             = true;

    // Whether the taxonomy is publicly queryable
    $args["publicly_queryable"] = true;

    // Whether the taxonomy is hierarchical
    $args["hierarchical"]       = false;

    // Whether to show the taxonomy in the admin menu
    $args["show_in_menu"]       = true;

    // Sets the query var key for this taxonomy
    $args["query_var"]          = true;

    // Triggers the handling of rewrites for this taxonomy
    $args["rewrite"]            = [ "slug" => "star" ];

    // Worpress function to register taxonomy
    register_taxonomy( "star", ["movie"], $args );
}
?>

Now we include custom_taxonomies.php in functions.php
<?php include( "custom_taxonomies.php");?>