Skip to content

Instantly share code, notes, and snippets.

@karks88
Created June 20, 2024 12:50
Show Gist options
  • Save karks88/41865baf25d2e2e559a73107426f4615 to your computer and use it in GitHub Desktop.
Save karks88/41865baf25d2e2e559a73107426f4615 to your computer and use it in GitHub Desktop.
ChatGPT explains a MySQL Query
To create a MySQL query that retrieves users with a WooCommerce Customer ID and a membership via Restrict Content Pro, you will need to join several tables: `wp_users`, `wp_usermeta`, `wp_wc_customer_lookup`, and `wp_rcp_memberships`.
Here is the query to achieve this:
```sql
SELECT
u.ID AS user_id,
um_first_name.meta_value AS first_name,
um_last_name.meta_value AS last_name,
wc.customer_id,
rcp.status,
rcp.object_id
FROM
wp_users u
INNER JOIN
wp_usermeta um_first_name ON u.ID = um_first_name.user_id AND um_first_name.meta_key = 'first_name'
INNER JOIN
wp_usermeta um_last_name ON u.ID = um_last_name.user_id AND um_last_name.meta_key = 'last_name'
INNER JOIN
wp_wc_customer_lookup wc ON u.ID = wc.user_id
INNER JOIN
wp_rcp_memberships rcp ON u.ID = rcp.user_id
WHERE
rcp.status = 'active'; -- Assuming you want only active memberships, adjust as needed
```
### Explanation:
- `wp_users u`: This is the main table containing user information. We alias it as `u`.
- `wp_usermeta um_first_name`: This table contains user meta information. We join it on the user ID and filter for the `first_name` meta key.
- `wp_usermeta um_last_name`: This is another join on the `wp_usermeta` table to get the `last_name` meta key.
- `wp_wc_customer_lookup wc`: This table contains WooCommerce customer information. We join it on the user ID.
- `wp_rcp_memberships rcp`: This table contains Restrict Content Pro membership information. We join it on the user ID and filter for active memberships if needed.
### Customizing the Query:
- If you want to include other statuses from the `wp_rcp_memberships` table, you can modify the `WHERE` clause accordingly.
- Make sure the table prefixes (`wp_`) match your actual WordPress table prefixes if they are different.
This query will give you the desired information about users who have both a WooCommerce customer ID and an active membership via Restrict Content Pro.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment