I’m in the middle of assisting a colleague in reconfiguring a WordPress database for use on another platform. The database is essentially details regarding all the posts and accompanying metadata, etc.
One of the things I’m stuck on is determining how to correlate each post with its WordPress category. Essentially, I’d like to be able to pull out every post with its corresponding category into a spreadsheet, and to be able to do that, I clearly need to know their relationship within the tables.
Can anyone point me in the correct direction? Any help would be sincerely appreciated. Thank you.
WordPress has a table called *wp_term_relationships* to keep track of several types of relationships, including post – category.
Let’s go table by table:
wp_posts – holds all posts,pages, and other content. You want to poll all rows that have -> post_type = post . You might want to poll only published posts -> post_status = publish
wp_term_relationships – The object_id column will hold the post id’s and the term_taxonomy_id will hold the category id.
wp_term_taxonomy – This table will hold the information of each taxonomy. Not all taxonomies are categories in wordpress. So you will want to poll only terms that have -> taxonomy = category.
wp_terms – This table holds the term information. You already have the term_id in the table above, but you don’t know it’s name. You can get it from here.
Your SQL statement will look like: