获取 WooCommerce 中没有特定分类的产品列表

Posted

技术标签:

【中文标题】获取 WooCommerce 中没有特定分类的产品列表【英文标题】:Get list of products that don't have a specific taxonomy in WooCommerce 【发布时间】:2018-05-31 12:09:39 【问题描述】:

我遇到了一个问题,试图在一个拥有数万种产品的网站上找出哪些产品缺少供应商。

我需要一个 mysql 查询来通过 phpmyadmin 确定哪些产品缺少分类“供应商”。

到目前为止,我已经从各种答案中拼凑出这个:

SELECT *
FROM wp_posts wp
INNER JOIN wp_postmeta wm ON (wm.`post_id` = wp.`ID`)
INNER JOIN wp_term_relationships wtr ON (wp.`ID` = wtr.`object_id`)
INNER JOIN wp_term_taxonomy wtt ON (wtr.`term_taxonomy_id` = wtt.`term_taxonomy_id`)
INNER JOIN wp_terms wt ON (wt.`term_id` = wtt.`term_id`)
AND wtt.`taxonomy` = 'suppliers'
WHERE post_type = 'product'
GROUP BY wp.ID

我尝试了很多东西,但无法让它发挥作用。

【问题讨论】:

【参考方案1】:

问题在于您的查询是您没有排除具有suppliers 的产品,该产品具有一些其他属性,因此您可以使用NOT EXISTNOT IN,我已经使用NOT EXIST 编写了查询.

SELECT *
FROM wp_posts wp
INNER JOIN wp_postmeta wm ON wm.`post_id` = wp.`ID`
INNER JOIN wp_term_relationships wtr ON (wp.`ID` = wtr.`object_id`)
INNER JOIN wp_term_taxonomy wtt ON wtr.`term_taxonomy_id` = wtt.`term_taxonomy_id`
INNER JOIN wp_terms wt ON wt.`term_id` = wtt.`term_id`
WHERE post_type = 'product'
  AND NOT EXISTS
    (SELECT `object_id`
     FROM `wp_term_relationships` AS wtr_inner 
     WHERE `term_taxonomy_id` IN
         (SELECT term_taxonomy_id
          FROM `wp_term_taxonomy`
          WHERE `taxonomy` = 'suppliers')
       AND wtr.object_id = wtr_inner.object_id )
GROUP BY wp.ID

如果你知道所有的suppliersterm_taxonomy_id,另一个版本会更快一些所以你可以将上面的查询修改为

SELECT *
FROM wp_posts wp
INNER JOIN wp_postmeta wm ON wm.`post_id` = wp.`ID`
INNER JOIN wp_term_relationships wtr ON (wp.`ID` = wtr.`object_id`)
INNER JOIN wp_term_taxonomy wtt ON wtr.`term_taxonomy_id` = wtt.`term_taxonomy_id`
INNER JOIN wp_terms wt ON wt.`term_id` = wtt.`term_id`
WHERE post_type = 'product'
  AND NOT EXISTS
    (SELECT `object_id`
     FROM `wp_term_relationships` AS wtr_inner 
     WHERE `term_taxonomy_id` IN (27,28,29) -- replace it will all suppliers term_taxonomy_id
       AND wtr.object_id = wtr_inner.object_id )
GROUP BY wp.ID

希望这会有所帮助!

【讨论】:

以上是关于获取 WooCommerce 中没有特定分类的产品列表的主要内容,如果未能解决你的问题,请参考以下文章

如何从 WooCommerce 产品属性分类中获取术语?

从特定类别获取 WooCommerce 产品

显示 WooCommerce 产品属性的自定义分类术语图像

如何在woocommerce中获取特定类别的所有产品?

ajax 上的 JS 警报添加到购物车以获取 Woocommerce 中的特定产品类别计数

如何从 WooCommerce 产品属性术语中获取元数据