WordPress 2.3 introduces our new taxonomy schema. This new schema replaces the categories, post2cat, and link2cat tables with three new tables that are more flexible. The first table is the terms table. It holds the basic information about a term.
term_id bigint(20) NOT NULL auto_increment,
name varchar(55) NOT NULL default '',
slug varchar(200) NOT NULL default '',
term_group bigint(10) NOT NULL default 0,
PRIMARY KEY (term_id),
UNIQUE KEY slug (slug)
“name” is simply the name of the term. “slug” is the name reduced to a URL friendly form. “term_group” is a means of grouping together similar terms. “term_id” is a unique ID for the term.
A term is not a category or tag on its own. It must be given context via the term_taxonomy table.
term_taxonomy_id bigint(20) NOT NULL auto_increment,
term_id bigint(20) NOT NULL default 0,
taxonomy varchar(32) NOT NULL default '',
description longtext NOT NULL,
parent bigint(20) NOT NULL default 0,
count bigint(20) NOT NULL default 0,
PRIMARY KEY (term_taxonomy_id),
UNIQUE KEY term_id_taxonomy (term_id,taxonomy)
The term_taxonomy table places a term within a taxonomy. This is what makes a term a category or tag (or both). “term_id” is the ID of a term in the terms table. “taxonomy” designates the taxonomy in which the term resides. The default taxonomies are “category”, “link_category”, and “post_tag”. “term_taxonomy_id” is a unique ID for the term+taxonomy pair. The rest of the fields provide information about the term in the context of the taxonomy. The “parent” field keeps track of hierarchical relationships between terms in the taxonomy. “description” provides a taxonomy specific description of the term. “count” tracks how many objects are associated with the term+taxonomy pair. Given a taxonomy of “category”, “count” tracks how many posts are in the category.
The final table, term_relationships, relates objects such as posts or links to a term_taxonomy_id from the term_taxonomy table.
object_id bigint(20) NOT NULL default 0,
term_taxonomy_id bigint(20) NOT NULL default 0,
PRIMARY KEY (object_id,term_taxonomy_id),
KEY term_taxonomy_id (term_taxonomy_id)
“object_id” is the ID of a post or link. “term_taxonomy_id” is an ID from the term_taxonomy table designating a particular term+taxonomy pair.
The flexibility of the schema and API means plugins can add new taxonomies and object types quite easily. One of the Summer of Code projects does just that. The flexibility also allows us to easily retrieve all objects associated with a given term regardless of taxonomy, retrieve all terms from all taxonomies for a given object, and convert all categories to tags with one query.
The taxonomy schema is hidden behind a fairly comprehensive taxonomy API. The category API remains as a backward compatibility layer on top of the taxonomy API. Plugins that use the category API should not require any changes when upgrading to 2.3. Plugins that perform direct SQL queries on the categories, link2cat, or post2cat tables will break, unfortunately.
And that’s the new schema. It will break a few plugins in the short term, but in the long term it will allow us to add any new taxonomies that come along without needing to change the schema again.