I have a really strange problem.
I simply want to select all posts that feature in two given categories.
Heres my SQL:
SELECT wp_posts.ID
FROM wp_posts
INNER JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
WHERE wp_term_relationships.term_taxonomy_id = 83
AND wp_term_relationships.term_taxonomy_id = 84
This should return one result but it doesnt return anything.
If I comment out:
*wp_term_relationships.term_taxonomy_id = 83*
The post im looking for is returned.
If I comment out:
*wp_term_relationships.term_taxonomy_id = 84*
Again, the post is returned.
Why then,when both of these conditions are included, nothing is returned?
Any help will be appreciated.Thanks
If you want to return all posts that have both those
term_taxonomy_id‘s associated with them you can use:This will return all records that have both taxonomy terms.
Your query will not work because the
term_taxonomy_idcannot have two values at the same time. You can change your query to useORinstead ofANDor you can use the version I provided.