Skip to content

Instantly share code, notes, and snippets.

@ideadude
Created October 28, 2023 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ideadude/e8c4c64c46c12d307767e02046dbb1f1 to your computer and use it in GitHub Desktop.
Save ideadude/e8c4c64c46c12d307767e02046dbb1f1 to your computer and use it in GitHub Desktop.
MySQL query to get members with paid levels (6, 20, 21) that don't have a paid order within the past 12 months.
# Find members with active paid memberships but their last order was > 12 months ago.
SELECT
m.user_id,
m.membership_id,
u.user_email,
MAX(o.timestamp) as last_order_timestamp
FROM
wp_pmpro_memberships_users m
LEFT JOIN wp_users u ON m.user_id = u.ID
JOIN
wp_pmpro_membership_orders o ON m.user_id = o.user_id
WHERE
m.membership_id IN (6, 20, 21)
AND m.status = 'active'
GROUP BY
m.user_id, m.membership_id
HAVING
last_order_timestamp < DATE_SUB(NOW(), INTERVAL 12 MONTH);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment