Create a custom WordPress taxonomy for posts and pages

By Dillon Smart · · · 0 Comments

Sometimes when building a website using WordPress, the standard taxonomies Categories and Tags just aren’t enough, and you need to create more. Let’s learn how to create our own custom WordPress taxonomy.

Wordpress Post Taxonomy

WordPress has made creating your own taxonomy easy, and you can even add your custom taxonomies to pages too. To accomplish this we use the custom WordPress taxonomy function register_taxonomy().

Creating a custom WordPress taxonomy

To create a new WordPress Taxonomy, we will need to use the register_taxonomy function which comes built-in to WordPress.

WordPress has hundreds of these functions you can use to customize your WordPress website. If you are unfamiliar with making changes to the code of your WordPress theme, or unfamiliar with PHP the programming language WordPress is built in, check out this post which explains and answers the question what is PHP?

In the snippet below, we create a new custom taxonomy called Services and add it to the Pages post type.

add_action( 'init', 'create_taxonomy' );
function create_taxonomy() {
    register_taxonomy(
        'Service',  
        'page',  
        array(
            'label' => 'Service', // the name of your new taxonomy
            'hierarchical' => true,
        )
    );
}

Copy and paste the snippet into your functions.php file, which is located in the root directory of your theme.

Notice in the code the comment next to ‘label’. You can change this to whatever suits your needs best. In this case, I want to create a new WordPress Taxonomy called Services.

Now, if you navigate to the WordPress admin area, and select pages, you will notice a new label called Services.

Conclusion

You can also read up on WordPress taxonomies here.

PHPWordPress

0 Comment

Was this helpful? Leave a comment!

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

Attempt to assign property post_content on null – Critical WordPress Error

Updated 28th December 2023

Are you seeing an error when creating new posts or pages in WordPress? Attempt to assign property post_content on null A bad database migration likely causes the error. I have experienced this error when exporting a WordPress database using the database export tool built into PHPStorm through an SSH tunnel to set up a WordPress

The ULTIMATE guide to setup Windows Subsystem for Linux (WSL) with Ubuntu, Apache2, MySQL and PHP

Updated 16th August 2022

Windows is now good for development! Yes! All thanks to Windows Subsystem for Linux (WSL). No more WAMP or Xampp, WSL can do everything you need. In this ultimate guide to setup Windows Subsystem for Linux with Ubuntu, Apache2, MySQL, and PHP we will cover: What is Windows Subsystem for Linux? Setting up Windows Subsystem

Pixel Tracking in PHP

Updated 16th August 2022

When creating email campaigns, it’s important to know how many recipients actually opened your email. This information can help you determine if your efforts are working or if it’s time to change your strategy. Most mail champaign tools such as Mailchimp and SendInBlue use a technique called Pixel Tracking. In this post, you will learn