Page no: Da08
Page no: Da08
Taxonomies are what takes WordPress beyond a simple blogging platform and into the realms of a CMS.
But how are they stored in the database?
Taxonomies and terms are a powerful WordPress feature which let you add much more flexibility to your sites. They are related to two other content types: posts and links, and the database structure means that this is a one-to-many relationship, where one post can have multiple terms across multiple taxonomies, and one term can be assigned to multiple posts or links.
I’ll start by defining them, as like so many things in WordPress, the terminology can be confusing!
A taxonomy is a system of categorizing or classifying things, normally hierarchically. The most famous taxonomy is the Linnean Taxonomy which is used to classify living things.
In WordPress, taxonomies are used to classify your data and group it into sets and subsets.
WordPress comes with three taxonomies built in:
- link category
Categories and tags are a bit like posts and pages in that they are the same type of content (taxonomies) but behave differently by default, in that categories are hierarchical and tags are not.
The link category taxonomy works in a similar way to tags and could theoretically be used for any object type, but by default it is not displayed in the post editing screen and is in the link editing screen if links have been enabled.
You can also add as many extra taxonomies as you want using custom taxonomies. These then behave in a similar way to categories and tags, and have terms. Each of your taxonomies then has the same status as any of the built-in taxonomies. This is similar in a way to a comparison between posts and custom post types.
Each taxonomy will have terms you use to sort your data. A category is just a term in the category taxonomy, and a tag is a term in the tag taxonomy. When you create taxonomies, you will then create terms for your taxonomies either via the WordPress dashboard or using the wp_insert_term() function.
Terms can be very powerful when combined with custom queries: you can create custom template files in your themes or plugins to display posts with multiple terms, sort by terms, identify terms across taxonomies and much more.
How WordPress Stores Taxonomies and Terms
As I described in the tutorial on relationships between data, WordPress uses a many-to-many relationship. This relationship is created by using three tables:
These tables are shown below, along with the tables they are linked to,
Let’s take a look at each of the tables and how it works.
The wp_terms Table
wp_terms table stores all of the individual terms for your categories, tags, links categories and custom taxonomies. It has just four fields:
term_idis the unique ID for the term
term_groupis a field that isn’t currently used by WordPress so you can safely ignore it.
The wp_term_taxonomy Table
wp_term_taxonomy table stores more data about terms as well as the taxomies they are part of. It has six fields:
term_taxonomy_idstores an ID for the record in this table
term_idrepresents the ID of the term, linked to its record in wp_terms
taxonomyis the name of the taxonomy which the term is in
parentrefers to the term’s parent term, if the taxonomy is hierarchical and it has one
countis the count of posts with the term
In many WordPress installations, there will be one record in the
wp_term_taxonomytable for each term in the
wp_terms table, but in some cases you will have more than one record for each term. This happens when you create two terms with the same name and slug in different taxonomies, and means that you could create a query to output posts with that term in multiple taxonomies.
This means that the relationship between these two tables is one-to-many: one record in the
wp_terms table can be linked to multiple records in the
wp_term_taxonomy table, but each record in
wp_term_taxonomy is only linked to one record in
The wp_term_relationships Table
wp_term_relationships table is crucial in creating the many-to-many relationship between objects and terms. It has just three fields:
object_idis linked to
term_taxonomy_idis linked to the same field in the
term_orderis the order in which terms were added to an object. This is only used if you specified the sort argument to be true when you registered the taxonomy – the default is false and the default value for this field is 0.
Because each object can be related to multiple records in the
wp_term_relationships table, and so can each term, this creates the many-to-many relationship.
The relationship between objects (i.e. posts and links) and terms is a very powerful one, partly because it’s the only many-to-many relationship used by WordPress.
Understanding how this relationship works and where the key data is stored will help you understand how taxonomies and terms work, and to use the functionsassociated with them.See more for