canned-338829_640

How To Programatically Add Terms to a WordPress Taxonomy

In the first post of this two part series we looked at how to add custom taxonomies to your theme. In this post we will cover how to add terms to your custom taxonomies (or any taxonomy for that matter), and also how to make your custom taxonomy behave like the Category taxonomy and allow you to add new terms directly from the add new/edit posts page in the dashboard.

I will not cover all the details in depth as this post is just meant to be a code snippet more than anything. I would recommend that you take a look at the codex page for taxonomies as a starting point to get more in depth information about coding with them.

When you register a taxonomy, you need to store the information for your taxonomy and register it on every new page cycle. However this is not the case with terms. When you register a term, this is stored in the database against the taxonomy name. If you try to re-register a term that already exists against the same taxonomy then you will get an error.

So the principle behind registering terms programatically is to check if the term exists and if it doesn’t then add it. This article assumes you have worked through the article on How to add a custom taxonomy to your theme and will use the code from that article as the starting point. If you have not looked at that article, you will need to do so before we proceed.

Back? cool, let’s take a look at the code.

Open up your functions.php and go to your my_custom_taxonomies function that you have written. We need to have some way of registering our terms against the associated taxonomies. My favourite way of doing this is by using a multi-dimensional associative array.

Now within the foreach loop, but after we have registered the taxonomy we need to add the code to register the terms for the taxonomy. We must also check to make sure that the term has not already been added otherwise we will get an error. The next code example shows how to do this.

This is a bit too simplistic for production purposes, and in a real life situation you would probably want to compare the terms already registered against those you are trying to register, as opposed to checking to see if any terms exist.

Now if you look at the management pages for Cowboys and Doctors you will see that your terms have been registered. However if you look at your custom taxonomies on the add new/edit posts page you can see that the new taxonomies still do not allow you to add terms directly from the page. Also if you click on the ‘choose from the most used’ link you get an error. One of the easier ways to correct these problems is to pass through the hierarchical option when you register your taxonomy.

Now when you look at the add new/edit posts page the new custom taxonomies behave more like the Category taxonomy, and allow you to select terms from the select box and display the most used terms. I will leave it up to you to find out what the hierarchical option does.

That concludes this article. There are other options you can use when registering a custom taxonomy, however this information is covered well in the WordPress codex.

Think you could have written this better? Then leave a reply and tell me what you think.