Wordpress mysql查询未返回子类别

Wordpress mysql查询未返回子类别,wordpress,woocommerce,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

我正在为另一个应用程序创建一个自定义查询,以从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')
一切正常,但返回的
类别名称
子类别名称
相同吗

这是一个返回的数组示例,
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\u category
category
是相同的?

快速浏览一下,您可能会在
块周围使用一组额外的括号。现在你有:

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'