Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pejantantangguh/d2eb6988ee3378362c312e33efc81aaf to your computer and use it in GitHub Desktop.
Save pejantantangguh/d2eb6988ee3378362c312e33efc81aaf to your computer and use it in GitHub Desktop.
Select Product name, SKU, price and category from WordPress / WooCommerce with MySQL query
SELECT
wp_posts.post_title AS Product,
wp_postmeta1.meta_value AS SKU,
wp_postmeta2.meta_value AS Price,
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_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