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

China☆狼群 提交于 2019-12-06 01:58:47
  1. select * from wp_blogs

From the output of the command note down the blog_id you want the users of. For eg: say you are wanting the users of the site with blog_id = 2 , next 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"
Trelawney

Thanks for this code, just what I needed!

If it helps anyone else, I also extended it a bit to get how many people have a given role, e.g. a custom role teacher was 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 other roles not to contain the word teacher (e.g. maybe there's a role ex-teacher which it would also pick up). That was the case for me so I ran two queries and subtracted the two numbers.

If anyone knows how to do it with one query that would be nice?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!