Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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()

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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>';
}
share|improve this answer
    
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 –  mattrepublic 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
    
That was the issue, I can't believe I overlooked that! Thanks @t31os! –  mattrepublic Aug 5 '11 at 0:15
    
Happy to help.. ;) –  t31os Aug 5 '11 at 0:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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