Understanding Taxonomy Relationships in WordPress
In WordPress for understand the relationship between the taxonomy tables, it’s helpful to look at a database diagram of the taxonomy table structure, as shown in Figure below.
You can the above image there are three taxonomy tables are joined together by unique IDs. Below a query to display all posts along with all taxonomy terms assigned to those posts:
SELECT wterms.name, wpost.post_title, wpost.post_date FROM wpost_terms wterms INNER JOIN wpost_term_taxonomy wttaxonomy ON wterms.term_id = wttaxonomy.term_id INNER JOIN wpost_term_relationships wtrelationships ON wttaxonomy.term_taxonomy_id = wtrelationships.term_taxonomy_id INNER JOIN wpost_posts wpost ON wtrelationships.object_id = wpost.ID WHERE wpost.post_type = ‘post’
Have you noticed that how you are joining on the table fields each other, as see in the above figure. The above example returns only three fields: post title, the taxonomy term and post date. The above query example returns all posts in your WordPress database along with all taxonomy terms attached to those posts.