custom mysql select statement for wordpress

admin

Administrator
Staff member
I have developed a taxonomy for a site that I've been working on that may be abusing the categorization system of wordpress- posts are categorized both under what topics they refer to (let's say cats, dogs, monkeys) as well as what type of post it is (say, expert, organization, article). So I'd like to find all posts that are about cats and dogs and that are organizations. Something along the lines of IN (cats, dogs) AND IN (organizations)... at least how it makes sense to me, but I can't figure out the right SQL syntax for the task.

Based on what I found in <a href="http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query" rel="nofollow noreferrer">this article on wordpress.com</a>, I'm building from the query below... but I'm not sure of the right syntax for how to say 'I want something that belongs to (either category 1 or 2) and (belongs to category 3) (say, cat 1=cats, 2=dogs, 3=organizations).

This is probably really simple and i'll be kicking myself when I get the response.

Code:
SELECT * FROM $wpdb-&gt;posts
LEFT JOIN $wpdb-&gt;postmeta ON($wpdb-&gt;posts.ID = $wpdb-&gt;postmeta.post_id)
LEFT JOIN $wpdb-&gt;term_relationships ON($wpdb-&gt;posts.ID = $wpdb-&gt;term_relationships.object_id)
LEFT JOIN $wpdb-&gt;term_taxonomy ON($wpdb-&gt;term_relationships.term_taxonomy_id = $wpdb-&gt;term_taxonomy.term_taxonomy_id)
WHERE $wpdb-&gt;term_taxonomy.term_id = 1,2,3
AND $wpdb-&gt;term_taxonomy.taxonomy = 'category'
AND $wpdb-&gt;posts.post_status = 'publish'
ORDER BY $wpdb-&gt;postmeta.meta_value ASC

Thanks!