title: Wordpress mysql query not returning subcategory
body: <p>I'm creating a custom query for another application to retrieve category and subcategory from the wordpress database. The wordpress data i'm attempting to retrieve is woocommerce products. </p>
  
  <pre><code> SELECT wp_posts.ID as product_code, v1.meta_value as sku,  v2.meta_value as price, v3.meta_value as product_attributes, wp_posts.post_title as product_name, terms_1.name as category_name, terms_2.name as sub_category_name, wp_posts.post_excerpt as description FROM wp_posts
  INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
  INNER JOIN wp_term_taxonomy terms_tax_1 ON (wp_term_relationships.term_taxonomy_id = terms_tax_1.term_taxonomy_id)
  LEFT JOIN wp_term_taxonomy terms_tax_2 ON (wp_term_relationships.term_taxonomy_id = terms_tax_2.term_taxonomy_id)
  INNER JOIN wp_terms terms_1 ON (terms_tax_1.term_id = terms_1.term_id)
  LEFT JOIN wp_terms terms_2 ON (terms_tax_2.term_id = terms_2.term_id)
  INNER JOIN wp_postmeta v1 ON (wp_posts.ID = v1.post_id AND v1.meta_key = '_sku')
  INNER JOIN wp_postmeta v2 ON (wp_posts.ID = v2.post_id AND v2.meta_key = '_price')
  INNER JOIN wp_postmeta v3 ON (wp_posts.ID = v3.post_id AND v3.meta_key = '_product_attributes')
  WHERE wp_posts.post_status = 'publish' AND wp_posts.post_type = 'product' AND 
  (terms_tax_1.taxonomy = 'product_cat' AND terms_tax_1.term_id='1646') OR (terms_tax_2.taxonomy = 'product_cat' AND terms_tax_2.parent='1646')
  </code></pre>
  
  <p>Everything is working but the <code>category_name</code> and <code>sub_category_name</code> returned are identical? </p>
  
  <p>This is an example of an array returned, the <code>sub_category_name</code> should be <code>Timber Framing</code></p>
  
  <pre><code> Array([product_code] =&gt; 12198
              [sku] =&gt; 12198
              [price] =&gt; 1.30
              [product_attributes] =&gt; a:1:{s:12:"size-options";a:6:{s:4:"name";s:12:"Size Options";s:5:"value";s:82:"50mm x 50mm Green Treated | 50mm x 75mm Green Treated | 50mm x 100mm Green Treated";s:8:"position";s:2:"24";s:10:"is_visible";i:1;s:12:"is_variation";i:1;s:11:"is_taxonomy";i:0;}}
              [product_name] =&gt; Budget Framing Timber
              [category_name] =&gt; Timber for Garden
              [sub_category_name] =&gt; Timber for Garden
              [description] =&gt; This budget Framing Timber completes all the essentials for creating an economical decking area in your garden. It has been specially produced and treated so it can withstand heavy use. Its natural construction will give your new decking area a natural look whilst blending well with the surroundings in your garden. Guaranteed our timber is 100% FSC certified and fully tanalised to 15KG/M3.
          )
  </code></pre>
  
  <p>Why are <code>sub_category</code> and <code>category</code> identical?</p>