wpdb get posts by taxonomy SQL

Sorry guys, I just found the solution:

SELECT p.post_name, t.name as clientName 
FROM $wpdb->posts AS p
INNER JOIN $wpdb->term_relationships AS tr ON ('p.ID' = tr.object_id)
INNER JOIN $wpdb->term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
INNER JOIN $wpdb->terms AS t ON (t.term_id = tt.term_id)
WHERE   p.post_status="publish" 
    AND p.post_type="portfolio"
    AND tt.taxonomy = 'clients' 
ORDER BY p.post_date DESC

I just don’t have to define an alias for $wpdb->terms in FROM clause.

Leave a Comment