Skip to content
Advertisement

SQL query to extract all WordPress posts with categories

I need to extract all posts from my WordPress DB along with the associated categories and not sure how to write this query. I’ve taken a couple of stabs at it already with no joy and would appreciate the help?

EDIT: Here’s what I have tried already:

This seems to work but it returns 1,553 where I know I only have 1343 in my DB.

EDIT: We did the same thing on another SQL query a little while ago and found that it was pulling in the revisions and other post types but thought that this was resolved using post_type = ‘post’

EDIT: Upon looking at the number of categories in the DB, I come up with a total number of 216, 6 off the number if you subtract 1553 – 1343 = 216. So I think this total number of 1553 is coming from the wp_terms table which needs to be excluded and only those that are active with published posts should be shown?

EDIT: The other possibility is that each post can have multiple categories, hence the reason for having more posts (1553). So how could I separate each posts into multiple categories?

Many thanks!

Advertisement

Answer

This is the final answer that worked for me.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement