0

I'm trying to make a table of users by role type. What is the best-practices method of getting the user id? My goal is to print each users first & last names, username and email.

What I have now:

$get_admins = array(
    'blog_id' => $GLOBALS['blog_id'],
    'role' => 'administrator',
);

$blogusers = get_users($get_admins);
foreach ($blogusers as $user) { 
$userid = get_userdata($bloguser->user_id);
$user_info = get_userdata($userid);

    echo '<tr>';
        echo '<td>'. $user_info->user_firstname .'</td>';
        echo '<td>'. $user_info->last_name .'</td>';
        echo '<td>'. $user->user_login .'</td>';
        echo '<td>'. $user->user_email .'</td>';
    echo '</tr>';
}

note: In my example above getting the user id does not work.

Codex reference: get_users()

2

Your code doesn't work because you're using $bloguser instead of $user.

And there's no point in calling get_userdata() since get_users() already returns user objects:

foreach ($blogusers as $user) {
    echo '<tr>';
        echo '<td>'. $user->first_name .'</td>';
        echo '<td>'. $user->last_name .'</td>';
        echo '<td>'. $user->user_login .'</td>';
        echo '<td>'. $user->user_email .'</td>';
    echo '</tr>';
}
  • Dropping the redundant code works for stuff stored in the wp_users table. First/last name though is meta and stored in wp_usermeta. [I'm sure you already know this :)] So using $user->user_firstname isn't putting out anything. What am I not understanding? Thanks @Scribu – torinagrippa Aug 5 '11 at 0:00
  • 1
    get_users performs a join to the meta table, and the fields param defaults to all so that meta data should come back in the query(unless a given value isn't currently set for the given user(s)). Verify the users you're not seeing a first name for definately have one set. – t31os Aug 5 '11 at 0:10
0

Your code does not work because in foreach loop get_userdata($bloguser->user_id); line, $bloguser is an unknown object variable. Please try this code:

$get_admins = array(
    'role'    => 'admin',
    'orderby' => 'user_nicename',
    'order'   => 'ASC'
);
$blogusers  = get_users( $get_admins );
echo '<ul>';
foreach ( $blogusers as $user ) {
    echo '<li>' . $user->first_name . '</li>';
    echo '<li>' . $user->last_name  . '</li>';
    echo '<li>' . $user->user_login . '</li>';
    echo '<li>' . $user->user_email . '</li>';
}
echo '</ul>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.