Skip to content

Instantly share code, notes, and snippets.

@kidino
Created November 4, 2021 03:48
Show Gist options
  • Save kidino/48ce7203d1e543df50edc29871a6c8cd to your computer and use it in GitHub Desktop.
Save kidino/48ce7203d1e543df50edc29871a6c8cd to your computer and use it in GitHub Desktop.
Query WP Users and User Meta (based on user roles) into Columns and Filtering
/*
in this example, we are querying users with specific roles and their billing phone numbers
see how we are creating dynamic columns with max and case
also how where are using HAVING to filter group data
*/
SELECT wp_users.display_name, wp_users.user_email,
max(case when (meta_key='billing_phone') then meta_value else NULL end) as 'billing_phone',
max(case when (meta_key='wp_capabilities') then meta_value else NULL end) as 'wp_capabilities'
FROM wp_usermeta LEFT JOIN wp_users ON wp_users.ID = wp_usermeta.user_id
group by wp_users.ID
HAVING wp_capabilities LIKE '%bootcamp%'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment