How to get users belonging to a WordPress Multisite blog (site) with SQL?

I need to get all users who have joined (are members) of a site (blog) in WordPress multisite. To complicate this, I am doing this outside of WordPress and have no access to the internal Wordpress functionality, so you need to build the SQL directly.

In English, SQL will break down as "get an array of user IDs of users who are members of site x" (where site refers to one of the WordPress Multisite sites).

I tried to go through the WordPress code to figure it out but am struggling with the above.

I don't need PHP, I can do this, just an example SQL statement.

Many thanks!

+3


source to share


2 answers


  • select * from wp_blogs

From the command note output down, blog_id

you want users. For example, for example, you want the site users using blog_id = 2, run the following query.



  1. select * from wp_users u join wp_usermeta um on u.id=um.user_id where um.meta_key="wp_2_capabilities"

+1


source


Thanks for this code, just what I need!

If this helps anyone else, I've also expanded it a bit to see how many people have a given role, for example. custom role teacher

used below:

select * from wp_users u join wp_usermeta um on u.id=um.user_id where um.meta_key="wp_2_capabilities" AND um.meta_value LIKE '%teacher%'

      



This, of course, requires that other roles do not contain a word teacher (for example, maybe a role ex-teacher

that he also picked up). This was the case for me, so I ran two queries and subtracted two numbers.

If anyone knows how to do this with a single query that would be nice?

0


source







All Articles