Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tivnet/ae7b9d590fc21730e8f5 to your computer and use it in GitHub Desktop.
Save tivnet/ae7b9d590fc21730e8f5 to your computer and use it in GitHub Desktop.
SQL: WooCommerce product IDs by stock status and backorders
SELECT _stock, _stock_status, _backorders, GROUP_CONCAT( PID )
FROM (
SELECT
post_id AS PID,
meta_value AS _stock,
(
SELECT meta_value
FROM wp_postmeta
WHERE post_id = PID
AND meta_key = '_stock_status'
) _stock_status,
(
SELECT meta_value
FROM wp_postmeta
WHERE post_id = PID
AND meta_key = '_backorders'
) _backorders
FROM `wp_postmeta`
WHERE meta_key = '_stock'
) v
GROUP BY _stock, _stock_status, _backorders
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment