Menü schliessen
Created: March 2nd 2021
Last updated: December 10th 2021
Categories: Php,  Wordpress
Author: Tim Fürer

WordPress: Make a Custom Taxonomy

Tags:  functions.php,  guide,  PHP,  wordpress
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

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.

Coding the WordPress Custom Taxonomy

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:

WordPress Custom Taxonomy Code example

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.