How To Create Custom Taxonomy in WordPress
How To Create Custom Taxonomy in WordPress
HOME
WORDPRESS HOW TO CREATE CUSTOM TAXONOMY IN WORDPRESS?
FOLLOW US
WordPress?
HOW TO
WORDPRESS
AMAN MEHRA
AUGUST 21, 2021
LEAVE A COMMENT
Name
SUBSCRIBE
RECENT POSTS
In the last article, we learned about how to create a custom post type? You can
check this tutorial for a quick view of the registration of custom post types.
So in this tutorial, I will show you how to create a custom taxonomy for the post CATEGORIES
types in wordpress. We will learn two methods to register the custom taxonomy, Git
one with the plugin and the second programmatically without the plugin.
How To
JavaScript
Table of Contents
Laravel
1
What is WordPress Taxonomy?
2
How to Create Custom Taxonomies in WordPress PHP
2.1
Method 1: Create a Custom Taxonomy – with plugin Python
2.2
Method 2: Create a Custom Taxonomy Programmatically – without ReactJS
plugin
WooCommerce
3
Display Custom Taxonomy
WordPress
Suppose you have a custom post type ‘courses‘, so categories and tags do not WooCommerce Single Page Visual
make sense for this post type. Hook Guide
For example, you have ‘course type’ taxonomy then further it would have ‘Web
Development’, ‘Affiliate Marketing’, and ‘SEO’.
As I mentioned above you will learn two methods to create (register) custom
taxonomies in WordPress.
In the first method, we will create with the plugin and in the second method, we
will do programmatically using the functions.php file without any plugins.
So there are many plugins that you can use to create your custom taxonomies
for your custom post type. The following list is the best plugins to create custom
taxonomies.
Toolset Types
Pods
These plugins also help to create your custom post type. These have a very
user-friendly interface and you can easily create and edit them.
Note: If you have deactivated or delete the plugin then you cannot access your
custom post type or taxonomies but your data still exist stored in the WordPress
database.
Ok, let’s create the custom taxonomy with a plugin. We will use Custom Post
Type UI plugin in this tutorial. You can use what you want.
First of all, you need a Custom Post Type UI (CPT UI) plugin installed in your
WordPress setup. If you have already installed this plugin then you can continue
on to the next point and if you haven’t installed it then please install and activate
it using this guide on how to install a WordPress plugin?.
When you finished the installation, you will see a new menu item (CPT UI) in
your admin dashboard. So navigate to CPT UI > Add/Edit Taxonomies. It will
show you the screen like the below image.
In the above image, you will see the first field under the Basic settings is
Taxonomy Slug. This slug will be used in the URL and by using this URL path
you can see all the posts under this taxonomy.
So, as according to this tutorial how to create a custom post type in WordPress,
we will take the taxonomy slug is ‘course_types’.
The next fields are Plural and Singular labels of taxonomy. These fields will be
the name of the taxonomy and we will take ‘Course Types’ and ‘Course Type’
respectively.
After it, you can auto-populate your taxonomy labels from singular and plural
labels under the Additional label section. You can also add all the fields by
yourself.
So, the next option about Attach to Post Type, means which post type that you
want to attach with this taxonomy. You will see all existing post types listed as
checkboxes. So you need to just check these post types option that you want to
be attached.
Now, the next step is Additional labels. You can fill all the fields with the
perspective label name. Or you can fill these fields automatically, as I said
earlier. You just have to click on the Auto-populate link then these fields will
auto-populate using the plural and singular label.
These labels fields will use in WordPress when you are adding/editing or
managing the content of that particular taxonomy.
Next, you can see the Settings section about the taxonomy. You can set up
these settings as per your requirements. Each option explain next to it what it
will do. So read the brief description about the field and set it up as you want.
In the above image, you can see that I set up True to make this taxonomy a
hierarchical taxonomy and it will work like standard posts categories (parent-
child relationship).
You can make this taxonomy as non-hierarchical taxonomy work like posts tags
by setting that field as False.
So, we made this hierarchical taxonomy, which means this taxonomy will have a
main course type and the other will have a sub-type under each course type.
There are more settings about the shown in admin UI, shown in nav menu,
delete with the user, has archive page, Hierarchical for the parent-child
relationship, show in the admin menu, menu position, menu icon, and what will
support the post type be, and etc. Read the description about each field and set
it up as you want.
OK, when you finished with all settings then now you can click on the Add
Taxonomy button to create a custom taxonomy.
Now you can see the new custom taxonomy under your post type. As the tutorial
case, we made a custom taxonomy for the ‘courses’ custom post type, which
you can see in the below image.
Hope you have successfully created your custom taxonomy. Now you can group
your Courses using this taxonomy of ‘Course Types’.
In this method, we will create a custom taxonomy without using any plugins. We
will use the functions.php file to do all coding to register the custom taxonomy
and will register_taxonomy() functions for this purpose.
As I said at the starting of the tutorial is that you will not access your custom
taxonomy or custom post type that you have created with the plugins. But if you
register your taxonomy using this method then you will not lose access to your
taxonomy until you delete that code.
To create the custom taxonomy without plugins, follow the below steps.
Make a function with the array of all the lables and settings options
Then use the register_taxonomy() with passing the all labels and
settings options as an arguments
So these are the steps to create the custom taxonomy for your custom post
type. Let’s look in-depth to explaining each point and register our taxonomy with
example code.
Hierarchical Taxonomy
In the first step, you need to open your functions.php file, so navigate this path
wp-content/themes/your_active_theme/functions.php and open it.
Add the following code in this file at the end of the file. And replace the
taxonomy name and labels as per your requirements.
Don’t forget to save your functions.php file. After saving you will see the new
taxonomy added to your post type.
Non-Hierarchical Taxonomy
If you want to create a Non-hierarchical taxonomy then you have to make small
changes in the above code. Then it will work like WordPress posts tags.
So, you need to set the value of hierarchical as false in the $args array variable
to register your taxonomy as a non-hierarchical taxonomy.
1 $args = array(
2 'labels' => $labels,
3 'hierarchical' => false,
4 'public' => true,
5 'show_ui' => true,
6 'show_admin_column' => true,
7 'show_in_nav_menus' => true,
8 'show_tagcloud' => false,
9 );
10 register_taxonomy( 'course_types', array( 'courses' ), $
After replacing the above code, save your file and check it. Now it will work like
standard tags taxonomy and you can make a tag cloud using the
wp_tag_could() function to show all the taxonomy values.
You have successfully created your custom taxonomy. Now you can show them
with your custom post type while listing a post loop.
You need to create a custom template and assign that template to a WordPress
page. You can also create a custom shortcode function and hook that using the
add_shortcode() function.
To create a custom template, create a new file inside your active theme folder
and the following comment line code at starting of the file.
After this line, you can add your custom code to show the list of custom posts
and taxonomies. See the sample code for getting the post data with taxonomy.
1 $args = array(
2 'post_type' => 'courses'
3 );
4
5 $post_query = new WP_Query($args);
6
7 if($post_query->have_posts() ) {
8 while($post_query->have_posts() ) {
9 $post_query->the_post();
10 ?>
11 <h2><?php the_title(); ?></h2>
12 <p><?php the_content(); ?></p>
13 <p><?php the_terms( get_the_ID(), 'course_typ
14 <?php
15 }
16 }
In the above code, we used the_terms() function to display the terms of custom
taxonomy.
get_theID() : Post ID
Hope you understand all the code and learn how to create custom taxonomy in
WordPress and you have created it successfully. If you still have any query
please let me know in the comment section.
Tweet on Twitter
Share on Facebook
LEAVE A REPLY
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Save my name, email, and website in this browser for the next time I comment.
POST COMMENT
Your Blog Coach is the best site Blog How to Add Taxonomy Images in
Name
for finding the solution to any WordPress?
Categories
issue related to coding and learn
How to Create Custom Taxonomy Your email address
more cool stuff and tricks. Contact
in WordPress?
About
How to Create Custom Post SUBSCRIBE
Types in WordPress?