When working with taxonomies in WordPress, it’s helpful to understand the taxonomy database schema. The WordPress database contains three tables for storing taxonomy data:
- wp_terms – stores all of your taxonomy terms
- wp_term_taxonomy – defines what taxonomy each term belongs to
- wp_term_relationships – cross-reference table that joins taxonomy terms with your content
Let’s look at an example database query joining the taxonomy tables. The below query will return all posts with all taxonomy terms assigned to each post:
SELECT wt.name, p.post_title, p.post_date FROM wp_terms wt INNER JOIN wp_term_taxonomy wtt ON wt.term_id = wtt.term_id INNER JOIN wp_term_relationships wtr ON wtt.term_taxonomy_id = wtr.term_taxonomy_id INNER JOIN wp_posts p ON wtr.object_id = p.ID WHERE p.post_type = 'post'
Using joins you can start to understand the relationship between the three taxonomy tables. I’m a visual person, so I created the below graphic to illustrate the taxonomy table relationships.
Taxonomies are an extremely powerful component of WordPress. Understanding the taxonomy database schema can be very helpful when building more complex WordPress websites. If you are interested in learning more about taxonomies in WordPress, check out my Professional WordPress book.