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!
Definitions
Taxonomies
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:
- category
- tag
- 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.
Terms
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:
wp_term_relationships
wp_term_taxonomy
wp_terms
These tables are shown below, along with the tables they are linked to, wp_posts
and wp_links
:
Let's take a look at each of the tables and how it works.
The wp_terms Table
The wp_terms
table stores all of the individual terms for your categories, tags, links categories and custom taxonomies. It has just four fields:
-
term_id
is the unique ID for the term name
slug
-
term_group
is a field that isn't currently used by WordPress so you can safely ignore it.
The wp_term_taxonomy Table
The wp_term_taxonomy
table stores more data about terms as well as the taxomies they are part of. It has six fields:
-
term_taxonomy_id
stores an ID for the record in this table -
term_id
represents the ID of the term, linked to its record in wp_terms -
taxonomy
is the name of the taxonomy which the term is in description
-
parent
refers to the term's parent term, if the taxonomy is hierarchical and it has one -
count
is the count of posts with the term
In many WordPress installations, there will be one record in the wp_term_taxonomy
table 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 wp_terms
.
The wp_term_relationships Table
The wp_term_relationships
table is crucial in creating the many-to-many relationship between objects and terms. It has just three fields:
-
object_id
is linked topost_id
in thewp_posts
table orlink_id
in thewp_links
table -
term_taxonomy_id
is linked to the same field in thewp_term_taxonomy
table -
term_order
is 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.
Summary
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 functions associated with them.
Comments