Get list of products that don't have a specific taxonomy in WooCommerce

时间:2017-12-18 07:05:34

标签: mysql wordpress woocommerce phpmyadmin product

I've hit an issue trying to find out what products are missing suppliers on a website with tens of thousands of products.

I need an MySQL query to determine which products are missing the taxonomy "suppliers" through phpmyadmin.

So far I've pieced this together from various answers:

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

I've tried numerous things and cannot get it to work.

1 个答案:

答案 0 :(得分:1)

The problem is with you query is that you are not excluding product that has if pickerview == "back paddock" do (this...) else if pickerview == "Front Paddock" do (this instead...) that that product has some other attributes, so you can use suppliers or NOT EXIST, I have written the query using NOT IN.

NOT EXIST

Another version which will be a bit faster if you know all the 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 So you can modify the above query as

term_taxonomy_id

Hope this helps!