SQL QUERY needed to get POST category (taxonomy) ? – MUST be SQL statement

You can try this SQL query for the taxonomy ‘mi_neighborhoods‘ and let’s take POST_ID = 1304

SELECT t.*, tt.* FROM wp_terms AS t 
INNER JOIN wp_term_taxonomy AS tt ON (tt.term_id = t.term_id) 
INNER JOIN wp_term_relationships AS tr ON (tr.term_taxonomy_id = tt.term_taxonomy_id) 
WHERE tt.taxonomy IN ('mi_neighborhoods') AND tr.object_id IN (1304) 
ORDER BY t.name ASC;

In general you can get it from this function:

wp_get_object_terms(1304, 'mi_neighborhoods'); 

EDIT:

Here is a query that gives you id/name/slug of all the terms belonging to the ‘mi_neighborhoods‘ taxonomy:

SELECT t.term_id, t.name, t.slug  
FROM wp_terms AS t 
INNER JOIN wp_term_taxonomy AS tt ON (t.term_id = tt.term_id) 
WHERE tt.taxonomy IN ('mi_neighborhoods') 
ORDER BY t.name ASC

and here is the same for all non-empty terms:

SELECT t.term_id, t.name, t.slug 
FROM wp_terms AS t 
INNER JOIN wp_term_taxonomy AS tt ON (t.term_id = tt.term_id) 
WHERE tt.taxonomy IN ('mi_neighborhoods') AND tt.count > 0
ORDER BY t.name ASC

Leave a Comment