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

--------------------------------------------------

>>> Share a link to this question, just copy and paste the code bellow on your web page <<<

<a href="https://www.wptricks.com/question/sql-query-joining-on-taxonomy-tables-not-returning-what-i-would-expect/">

SQL query joining on taxonomy tables not returning what I would expect</a>
0
, , Anthony 1 week 0 Answers 44 views 0

Leave an answer