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

Eddie

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.

Raunak Gupta

The problem is with you query is that you are not excluding product that has suppliers that that product has some other attributes, so you can use NOT EXIST or NOT IN, I have written the query using 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

Another version which will be a bit faster if you know all the suppliers term_taxonomy_id So you can modify the above query as

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

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get WooCommerce products list that have similar SKU

Wordpress don't see taxonomy page

Get all WooCommerce products from a category that have specific meta data

Get all Nodes that don't have a specific relationship in Neo4j

WooCommerce Brand taxonomy terms list in a dropdown selector

Using a custom taxonomy for products shortcode in Woocommerce 3.3

List materials, which don't have a specific language key

WooCommerce get products based on specific taxonomy

How to get products list by products ids in woocommerce

Wordpress - woocommerce - myaccount/orders-list.php - get products images

WooCommerce Subscription: Don't have the SUSPEND button

Limit WooCommerce products in cart only from one custom taxonomy

Get records which don't have relation with specific id

WooCommerce: Get total of out of stock products by taxonomy term

Woocommerce: Mandatory coupon for specific products

Get products from a specific brand in WooCommerce using a WP_Query

Get a list of directories or files I don't have permission to read

sylius how to get products by taxonomy?

Get rows which do have references to list of values and at the same time don't have any references to list of other values

Woocommerce hide the price for specific products

How to get all posts if post don't have specific meta key?

Get cart items quantities from products with specific tag in WooCommerce

Get a list of users that are associated with taxonomy

How to get all products that doesn't have feature image in woocommerce?

WooCommerce get product attribute taxonomy value

Is it possible to get a list of tables which don't have a specific extended property?

Get ACF Image field from Custom Taxonomy loop/list (woocommerce)

Handling a custom taxonomy in WooCommerce wc_get_products function

Use custom taxonomy to select Related Products in Woocommerce