Could we help you? Please click the banners. We are young and desperately need the money
In this guide, I will show you how to make a basic Custom Taxonomy for WordPress.
Custom Taxonomies function simillar to post tags with the advantage being that you can further customize them. You use these together with Custom Post Types. If you do not know how to make Custom Post Types, consider reading this guide we have made, first.
If you know how to make Custom Post Types already, this process should be familiar to you since the way those are created is pretty much the same as Custom Taxonomies.
Head into your functions.php file.
First, we will have to declare the function:
function register_custom_taxonomy() {
For the next step, we will create a variable called "$args" and add some arguments to it:
$args = array(
'labels' => array(
'name' => __( 'Items Custom Taxonomy', 'taxonomy general name' ),
'singular_name' => __( 'Item Custom Taxonomy', 'taxonomy singular name' ),
'search_items' => __( 'Search Custom Taxonomies' )
),
'hierarchical' => true,
'query_var' => true,
'show_admin_column' => true,
'show_ui' => true,
'rewrite' => array( 'slug' => 'my-custom-taxonomy' )
);
Note that this basic example shows a Custom Taxonomy of the hierarchical type and that there are many more arguments available than shown here. For a full list of arguments you can add, check out the official WordPress Documentation for Custom Taxonomy arguments.
Now, to end the function, we will also have to register the Custom Taxonomy using the "$args" variable that we have created before:
register_taxonomy( 'my_custom_taxonomy', array('my-custom-post-type'), $args );
}
As you can see, we also specify which Custom Post Type we attach this Custom Taxonomy to.
Hook the function and you are good to go (this hook must be run while initialising, hence why we also add the 'init'):
add_action( 'init', 'register_custom_taxonomy' );
This is how your complete code might look like:
A further guide on making your own Custom Metabox for your Custom Taxonomy together with Advanced Custom Fields and what advantages you get over the default WordPress Metabox, can be found here.