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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取WooCommerce中没有特定分类的产品列表相关的知识,希望对你有一定的参考价值。

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

我需要一个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

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

答案

问题是你的查询是你不排除产品有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

另一个版本,如果你知道所有的suppliers term_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 产品属性术语中获取元数据