title: Wordpress Filter Products by Tag and Category
body: <p>I am working on a small wordpress hobby project and I have run into a bit of a snag. I have implemented WooCommerce and am using Category and Tags for my data.</p>
  
  <p>I have some entries which I know relate so for an example, the data would model as follows:</p>
  
  <p>Car #1:
  Category => Sedan
  Tag => Volvo</p>
  
  <p>Car #2:
  Category => Sedan
  Tag => Volvo</p>
  
  <p>Car #3:
  Category => Sedan
  Tag => BMW</p>
  
  <p>I am trying to filter these WooCommerce products to ensure that both tag and category match so in this instance, Car #1 is related to Car #2 and Car #3 is an Orphan.</p>
  
  <p>The current query that is being generated when getting related products is as follows:</p>
  
  <pre><code>SELECT * FROM wp_posts p   
  INNER JOIN wp_postmeta pm ON ( pm.post_id = p.ID AND pm.meta_key='_visibility' )  
  INNER JOIN wp_term_relationships tr ON (p.ID = tr.object_id) 
  
  INNER JOIN wp_term_taxonomy tt ON(tr.term_taxonomy_id = tt.term_taxonomy_id)  
  INNER JOIN wp_terms t ON (t.term_id = tt.term_id)  
  WHERE (( tt.taxonomy = 'product_cat' AND t.term_id IN ( 14 ))) 
  OR  (( tt.taxonomy = 'product_tag' AND t.term_id IN ( 44) ));
  </code></pre>
  
  <p>The query gives me the correct result when I use OR however with more data I then get results which only satisfy one criteria which doesn't work well. I think the issue is that it is only fetching one taxonomy row per post so when I use AND (in where conditions) instead of OR in the query above, it doesn't work.</p>
  
  <p>Does anyone know of a solution to this or has experiencing a similiar issue? Alternatively is there a better way that I should structure the products?</p>
  
  <p>Thanks!</p>