1

For some reason this is not working for me :(

$get_members = array(
    'blog_id' => $GLOBALS['blog_id'],
    'role' => 'sm_flagar',
);

$blogusers = get_users($get_members);
    foreach ($blogusers as $user) {
        echo "<li><a href=\"".$user->user_url."\">". $user->first_name ." ". $user->last_name ."</a></li>";
    }

1 Answer 1

7

first_name and last_name are stored in the usermeta table. Therefore you have to use get_user_meta() to return these data. Try this code snippet:

$users = get_users(array(
    // blog_id is not required and will be set by WP_User
    'role' => 'sm_flagar'
));

foreach ($users as $user) {
    $firstName = get_user_meta($user->ID, 'first_name', true);
    $lastName = get_user_meta($user->ID, 'last_name', true);

    echo '<li><a href="' . $user->user_url . '">' . $firstName . ' ' . $lastName . '</a></li>' . PHP_EOL;
}
1
  • But I think get_user_meta() will hit query again on database which will slow down the process if users are too many. Is there any other way to get all data in single query? Commented Mar 23, 2017 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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