SQL select of users by metadata

Double-check your SQL syntax. It sounds like you want to do a JOIN … But you’re not building the query correctly.

It should be something more like:

SELECT u.ID, u.user_login, u.user_nicename, u.user_email
FROM $wpdb->users u
INNER JOIN $wpdb->usermeta m ON m.user_id = u.ID
WHERE m.meta_key = 'wp_capabilities'
AND m.meta_value LIKE '%supplier%'
ORDER BY u.user_registered

You have to tell the query how you’re relating the user meta to the user.

Also note that I’m using $wpdb-> rather than wp_. This is important if you ever expect to use this query in a plugin on a site with a database prefix other than “wp.” If you’re running things directly in SQL, though, you should switch back.

Leave a Comment

tech