Skip to content

Instantly share code, notes, and snippets.

@lukecav
Last active January 7, 2023 15:29
Show Gist options
  • Save lukecav/3c5b26c5272a83a290e9f02b1a0ed5c6 to your computer and use it in GitHub Desktop.
Save lukecav/3c5b26c5272a83a290e9f02b1a0ed5c6 to your computer and use it in GitHub Desktop.
Get all Products from WooCommerce (product title, sales price, regular price, stock, weight, sku and product categories) in the database using a MySQL query
SELECT
wp_posts.post_title AS Product,
wp_postmeta1.meta_value AS SKU,
wp_postmeta2.meta_value AS Price,
wp_postmeta3.meta_value AS Sales Price,
wp_postmeta4.meta_value AS Stock,
wp_postmeta5.meta_value AS Weight,
GROUP_CONCAT( wp_terms.name ORDER BY wp_terms.name SEPARATOR ', ' ) AS ProductCategories
FROM wp_posts
LEFT JOIN wp_postmeta wp_postmeta1
ON wp_postmeta1.post_id = wp_posts.ID
AND wp_postmeta1.meta_key = '_sku'
LEFT JOIN wp_postmeta wp_postmeta2
ON wp_postmeta2.post_id = wp_posts.ID
AND wp_postmeta2.meta_key = '_regular_price'
LEFT JOIN wp_postmeta wp_postmeta3
ON wp_postmeta3.post_id = wp_posts.ID
AND wp_postmeta3.meta_key = '_sale_price'
LEFT JOIN wp_postmeta wp_postmeta4
ON wp_postmeta4.post_id = wp_posts.ID
AND wp_postmeta4.meta_key = '__stock'
LEFT JOIN wp_postmeta wp_postmeta5
ON wp_postmeta5.post_id = wp_posts.ID
AND wp_postmeta5.meta_key = '_weight'
LEFT JOIN wp_term_relationships
ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_term_taxonomy
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.taxonomy = 'product_cat'
LEFT JOIN wp_terms
ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'product'
AND wp_posts.post_status = 'publish'
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment