MySQL Query to Get WordPress Post Categories

Spread the love

Here is pretty straight forward mysql query to get WordPress post categories. I’ve excluded the default Uncategorized category.

SELECT wt.*, wtt.taxonomy FROM wp_terms wt
inner join wp_term_relationships wtr on wtr.term_taxonomy_id = wt.term_id
inner join wp_term_taxonomy wtt on wtt.term_taxonomy_id = wtr.term_taxonomy_id
where wt.term_id <> 1 and wtr.object_id = 1

“wt.term_id <> 1″ to exclude Uncategorized Category and “wtr.object_id = 1” to get categories for post id 1.