Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@devinsays
Last active December 1, 2023 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save devinsays/f7041bb5747007f6612ac5d9c532d08c to your computer and use it in GitHub Desktop.
Save devinsays/f7041bb5747007f6612ac5d9c532d08c to your computer and use it in GitHub Desktop.
SQL: Get WooCommerce Product ID, title, SKU, and Image
SELECT
products.id,
products.post_title as title,
meta3.meta_value as sku,
meta2.meta_value as image_url
FROM
wp_qftw_posts products
LEFT JOIN
wp_qftw_postmeta meta1
ON (
meta1.post_id = products.id
AND meta1.meta_value IS NOT NULL
AND meta1.meta_key = "_thumbnail_id"
)
LEFT JOIN
wp_qftw_postmeta meta2
ON (
meta1.meta_value = meta2.post_id
AND meta2.meta_key = "_wp_attached_file"
AND meta2.meta_value IS NOT NULL
)
LEFT JOIN
wp_qftw_postmeta meta3
ON (
meta3.post_id = products.id
AND meta3.meta_key = "_sku"
AND meta3.meta_value IS NOT NULL
)
WHERE
products.post_status="publish"
AND products.post_type="product"
ORDER BY
products.post_date DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment