SQL query joining on taxonomy tables not returning what I would expect

Question

I want to get all posts that have a particular term of taxonomy A and a particular term of taxonomy B.

Here is the query I’m running:

        SELECT wp_posts.ID, wp_posts.post_title 
        FROM wp_posts 
        INNER JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id 
        INNER JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id 
        INNER JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id 
        WHERE (wp_term_taxonomy.taxonomy = 'category' AND wp_terms.slug in ('uncategorized'))
        AND (wp_term_taxonomy.taxonomy = 'post_tag' AND wp_terms.slug in ('red'))
        ORDER BY ID DESC

This returns 0 results despite querying on a DB / WP install that has a post with a category of “uncategorized” and a tag of “red”

enter image description here

0
, , Anthony 4 years 2020-03-26T16:52:45-05:00 0 Answers 113 views 0

Leave an answer

Browse
Browse