Skip to content

Instantly share code, notes, and snippets.

@yanknudtskov
Created June 25, 2019 07:17
Show Gist options
  • Save yanknudtskov/a75b98adb4bc475f296a09698b98a441 to your computer and use it in GitHub Desktop.
Save yanknudtskov/a75b98adb4bc475f296a09698b98a441 to your computer and use it in GitHub Desktop.
An easy way to get all products in WooCommerce which doesn't have a category, using SQL
# Gets all product category term IDs
SELECT wp_terms.term_id
FROM wp_terms
LEFT JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
WHERE wp_term_taxonomy.taxonomy = 'product_cat'
# Use the list of IDs from above into IN ( ) using commaseparation
# This will return all the product IDs which are associated to product categories
SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN( )
# Use the list of IDs from above into IN ( ) using commaseparation
# This will return all the products that are published and NOT found associated with a product category
SELECT * FROM wp_posts WHERE post_type = 'product' AND post_status = 'publish' AND ID NOT IN ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment