The title says it all. At the moment in my custom taxonomies I’m getting the terms using the term id and taxonomy name.
Thought this question would’ve been asked before but can’t find it anywhere! So just thought I’d ask and see if anyone has any answers.
If you take a look into the WordPress documentation you will find WordPress Taxonomies
The Codex says:
What does this mean?
A term is a word. It can belong to a taxonomy, such as tags, categories, or a custom taxonomy. The thing is: there can be several taxonomies containing the same term.
Letâs say you have a term called âfatteningâ. This word has an id number. This is the term_id. It doesnât depend on how this word is used, i.e. in which taxonomies the term appears.
Now the word âfatteningâ as a post tag also has a number. This is the term_taxonomy_id. It corresponds to âthe post tag âfatteningââ.
Maybe you also have a category called âfatteningâ. While the term_id is the same, the term_taxonomy_id for âthe category âfatteningââ is different.
As this is a part of a bigger understanding of the design I will describe it as whole… 🙂
In WP 4.5.3 there are still all these tables (I will talk about them without prefix):
The path to get post terms’ readable names goes through them all.
posts
the main identificator here is
ID
– an id of a post (of any type)term_relationships
stores pairs of:
object_id
– can beposts.ID
(but does not have to be)term_taxonomy_id
– this is NOT id of a term (category) but an id of RELATIONSHIP between a term(category) and taxonomy(“category type”)term_taxonomy
the main identificator here is
term_taxonomy_id
desribed above ^^another important columns:
term_id
– an id of a term (category)taxonomy
– stores the term’s taxonomy(“category type”)This one might seem funny, but the initial intend was to add the ability for terms to have more taxonomies (which in some cases can make sense).
terms
the main identificator here is the
term_id
– an id of an categoryanother important columns here are:
name
– readable category name e.g. “Music Genres”slug
– the slug of a term usable e.g. in URLSo the brutal demonstrating SQL to
get all published posts and all their categories with categories’ names
could look like this (add prefixes to tables when testing on your own WP DB):
The
term_id
is always unique just like theterm_taxonomy_id
.Both have an
Auto Increment
in the table structure.The table
wp_term_taxonomy
makes sure for example that a category is created, so wordpress can interfere with it e.g Woocommerce uses:product_tag
,product_type
,product_cat
, all of them are the so called custom taxonomies.This table also binds the parent/children structure. With it’s column
parent
.If we pick Woocommerce again as an example, this table links post type
products
to their taxonomyproduct_cat
, which contains a namy e.g Computers which is of course retrieved from the tablewp_terms
. This table contains theterm_id
,name
,slug
andterm_group
. (term_group I personally have never used)Hopefully the above makes things a bit more clear about the differences and how WordPress uses these tables.
The Article “Understanding and Working with Taxonomies and Terms in WordPress” explains the reason for using two separate tables –
wp_term
andwp_term_taxonomy
: