2015-04-07 65 views
0

我創建了另一個應用程序的自定義查詢來檢索wordpress數據庫中的類別和子類別。我試圖檢索的wordpress數據是woocommerce產品。Wordpress mysql查詢不返回子類別

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') 

一切工作,但category_name和返回sub_category_name是相同的?

這是返回的陣列的實施例中,sub_category_nameTimber Framing

Array([product_code] => 12198 
      [sku] => 12198 
      [price] => 1.30 
      [product_attributes] => 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] => Budget Framing Timber 
      [category_name] => Timber for Garden 
      [sub_category_name] => Timber for Garden 
      [description] => 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. 
     ) 

爲什麼sub_categorycategory相同?

回答

0

快速瀏覽一下,看起來好像你可以在你的OR塊周圍使用一組額外的括號。現在你已經有了:

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') 

而且你可能想:

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') 
    ) 

編輯

關於第二個想法,您的查詢可能會更好地使用子查詢代替連接。此查詢請求所有產品,然後可選查找與其關聯的類別和子類別

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, 
    (SELECT wp_terms.name FROM wp_term_taxonomy LEFT JOIN wp_term_relationships ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id LEFT JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id WHERE wp_term_taxonomy.term_id = 1646) as category_name, 
    (SELECT wp_terms.name FROM wp_term_taxonomy LEFT JOIN wp_term_relationships ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id LEFT JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id WHERE wp_term_taxonomy.parent = 1646) as sub_category_name, 
    wp_posts.post_excerpt as description 
FROM 
    wp_posts 
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'