Skip to content

Instantly share code, notes, and snippets.

@razajahangir
Last active January 27, 2026 08:58
Show Gist options
  • Select an option

  • Save razajahangir/5370ec70554df95dc2440c70a2d10449 to your computer and use it in GitHub Desktop.

Select an option

Save razajahangir/5370ec70554df95dc2440c70a2d10449 to your computer and use it in GitHub Desktop.
Advanced SQL query to export WooCommerce orders (Date, Email, Products, Coupons) from HPOS-enabled tables (wp_wc_orders) by joining with legacy item tables. Optimized for reporting.
SELECT
o.id AS Order_ID,
o.status AS Order_Status, -- Added Order Status here
o.date_created_gmt AS Order_Date,
a.email AS Billing_Email,
(
SELECT GROUP_CONCAT(order_item_name SEPARATOR ' | ')
FROM wp_woocommerce_order_items oi
WHERE oi.order_id = o.id
AND oi.order_item_type = 'line_item'
) AS Products,
(
SELECT GROUP_CONCAT(order_item_name SEPARATOR ' | ')
FROM wp_woocommerce_order_items oi
WHERE oi.order_id = o.id
AND oi.order_item_type = 'coupon'
) AS Coupons_Used
FROM
wp_wc_orders o
INNER JOIN
wp_wc_order_addresses a ON o.id = a.order_id
WHERE
o.type = 'shop_order'
AND o.date_created_gmt >= '2025-12-01 00:00:00'
AND o.date_created_gmt <= '2025-12-31 23:59:59'
AND a.address_type = 'billing';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment