• Resolved zagreus

    (@zagreus)


    I’m trying to retrieve users for a blog including their roles. When I use this, I now get an error. I’m pretty sure this worked previously before the 4.6 update.

    $args = array(
                'blog_id' => get_current_blog_id(),
                'fields' => array('id', 'display_name', 'user_email', 'roles'));
    
            $users = get_users($args);

    Here’s the error I get:

    WordPress database error Unknown column 'wp_users.roles' in 'field list' for query SELECT wp_users.id,wp_users.display_name,wp_users.user_email,wp_users.roles FROM wp_users INNER JOIN wp_usermeta ON ( wp_users.ID = wp_usermeta.user_id ) WHERE 1=1 AND (
      wp_usermeta.meta_key = 'wp_capabilities'
    ) ORDER BY user_login ASC  made by require('C:\MAMP\htdocs\web\wp\wp-blog-header.php'), wp, WP->main, WP->parse_request, do_action_ref_array, rest_api_loaded, WP_REST_Server->serve_request, WP_REST_Server->dispatch, Trainerous_Mobile_Public->{closure}, Trainerous_Mobile_Public->users_get, get_users, WP_User_Query->__construct, WP_User_Query->query

    As always, any suggestions / help much appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • It is my understanding that “roles” is now a somewhat secondary concept, taking a back seat to “capabilities”.

    You could setup a localhost server and experiment with different versions, which would give some indications as to which revisions you need. As well as answer your question as to when it broke.

    Otherwise I can just suggest that you browse various plugins to put together what you need.

    Moderator bcworkz

    (@bcworkz)

    ‘roles’ is no longer a valid field argument. If you want to get meta fields (of which capabilities is one), you have to get everything, you cannot pick and choose.
    'fields' => 'all_with_meta',

    As Ross indicated, checking roles is not the whole picture. You need to check capabilities because users can be assigned capabilities beyond what’s available from their role. Roles should be thought of as a pre-defined, subject to change, container of capabilities. Any role can have capabilities added or removed. Just checking if the role is ‘Author’ for example, tells us nothing. Author in one installation could be equivalent to subscriber or editor in another installation. Always check for specific capabilities.

    Thread Starter zagreus

    (@zagreus)

    Makes sense, thank you all for your comments.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Retrieve users and roles gives error’ is closed to new replies.