How to get all admins of multisite in wordpress

瘦欲@ 提交于 2019-12-11 05:51:48

问题


How to get all admins of multisite in wordpress. I am create custom plugin for this I am stuck in custom code for get all the admins of multisite in main website. for ex. my main website is : wyz.com and my second site is : xyz.com/demo. main website admin is "abc" and for " xyz.com/demo" site admin is "abcde". Now how i get "abcde" admin in my main website .

In my live site i have currently 6k admins. So i difficult to get this . I am new in multisite wordpress.


回答1:


To get the data from any of the sub site, you first need to switch to that site using switch_to_blog() function. Then whatever the query you fire, it will give records from that site only. Don't forget to restore it to current site, once you get the data from sub-site. You can restore it using restore_current_blog() function.

To get the all admin users of all the sites, you need to perform the followings:

1) Use wp_get_sites() function to get the blog_id of all the sites.

2) Once you get the blog_id, You need to perform the following loop to get the admin user of each the sites.

Assume that you get $blog as array of blog_ids from wp_get_sites() function

foreach ($blogs as $blog) 
    {
        switch_to_blog( $blog->blog_id ); // blog id which u got from wp_get_sites() function
        $users_query = new WP_User_Query( array( 
                    'role' => 'administrator', 
                    'orderby' => 'display_name'
                    ) );  // query to get admin users
        $results = $users_query->get_results();
        $site_admins .= 'Blog ID: ' . $blog->blog_id . '<pre>' . print_r($results,true) . '</pre>';
    }
    restore_current_blog();


来源:https://stackoverflow.com/questions/47009641/how-to-get-all-admins-of-multisite-in-wordpress

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