Wordpress mysql查询不返回子类别

时间:2015-04-07 13:04:42

标签: wordpress woocommerce

我正在为另一个应用程序创建自定义查询,以从wordpress数据库中检索类别和子类别。我试图检索的wordpress数据是woocommerce产品。

 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_namesub_category_name是相同的?

这是返回数组的示例,sub_category_name应为Timber 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相同?

1 个答案:

答案 0 :(得分: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'