In Wordpress, I am attempting to operate a query to come back a postID where it's been labeled by two (both) 'tags'. I believe I might require a subquery of some kind. Any help appreciated, heres things i have to date.

SELECT wposts .id
 FROM wp_posts wposts
INNER JOIN wp_term_relationships ON(wposts.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  wposts.post_status = 'publish' AND wposts.post_type = 'header-image' AND wp_term_taxonomy.taxonomy = 'tags' AND ( ='homepage' AND ='position1' )

do this:


// The Query
$the_query = new WP_Query( 'tag=TAG1+TAG2' );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();        

// Reset Post Data


More information here:

And when you're working directly using the database in Wordpress make use of this:

SELECT p .id

FROM wp_posts AS p
  INNER JOIN wp_term_relationships AS rel1
    ON p.ID = rel1.object_id
  INNER JOIN wp_term_taxonomy AS tax1 
    ON rel1.term_taxonomy_id = tax1.term_taxonomy_id
  INNER JOIN wp_terms AS term1 
    ON tax1.term_id = term1.term_id

  INNER JOIN wp_term_relationships AS rel2
    ON p.ID = rel2.object_id
  INNER JOIN wp_term_taxonomy AS tax2 
    ON rel2.term_taxonomy_id = tax2.term_taxonomy_id
  INNER JOIN wp_terms AS term2 
    ON tax2.term_id = term2.term_id

WHERE p.post_status = 'publish' 
  AND p.post_type = 'header-image' 
  AND tax1.taxonomy = 'tags' 
  AND ='homepage'
  AND tax2.taxonomy = 'tags'  
  AND ='position1'