WordPress – Display ALL users with custom meta, excluding certain user IDs

I am trying to display a list of ALL users on a page. It needs to display their name, email address, website URL, and several custom author meta fields.

I started with the following query:

Read More
<?php
//displays all users with their avatar and their posts (titles)
$blogusers = get_users_of_blog();
if ($blogusers) {
  foreach ($blogusers as $bloguser) {
    $user = get_userdata($bloguser->user_id);
    echo get_avatar( $user->ID, 46 );
    echo '<div><p>User ID ' . $user->ID . ' ' 
                            . $user->user_firstname . ' ' 
                            . $user->user_lastname . ' ' 
                            . $user->user_url . ' ' 
                            . $user->user_description 
             . '</p></div>';
    $args=array(
      'author' => $user->ID,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );

  }
}
?>

This diplays the basic user info, but no custom author meta fields. I tweaked it to this:

<?php
//displays all users with their avatar and their posts (titles)
$blogusers = get_users_of_blog();
if ($blogusers) {
  foreach ($blogusers as $bloguser) {
    $user = get_userdata($bloguser->user_id);
    echo get_avatar( $user->ID, 46 );
    echo '<div><p>User ID ' . $user->ID . ' ' 
                            . $user->user_firstname . ' ' 
                            . $user->user_lastname . ' ' 
                            . $user->user_url . ' ' 
                            . $user->user_description 
           . '</p>'; ?>
        <?php the_author_meta('position', $user);?>
        <?php the_author_meta('telephone');?>
        <?php the_author_meta('linkedin');?>
        <?php the_author_meta('vcard');?>
    <?php echo '</div>';?><?php 
    $args=array(
      'author' => $user->ID,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );

  }
}
?>

This works for the first user, but for the second user onwards, it shows the first user’s custom author meta data.

I have spent an hour trying to figure out how to modify it further to display EACH users own custom author meta data, however I cannot get it to work.

How can this be modified?

Also, I need to be able to EXCLUDE certain user ids – I tried adding ‘exclude’ and entering the ID’s I wanted to exclude, but it did not work.

Please help

Related posts

Leave a Reply

3 comments

  1. To get a specific user’s meta using the_author_meta() function you need to pass the user id, i.e.

    php the_author_meta('telephone' 25); // will get the telephone field's value of user with id 25
    

    So in your code you can use

    <?php the_author_meta('position', $user->ID);?><?php the_author_meta('telephone', $user->ID);?><?php the_author_meta('linkedin', $user->ID);?><?php the_author_meta('vcard', $user->ID);?>
    

    To exclude some users you can check user ids using in_array function, i.e.

    $excluded_users = array(10, 11, 12); // User Ids to exclude
    foreach ($blogusers as $bloguser) {
        if (!in_array($bloguser->user_id, $excluded_users))
        {
            $user = get_userdata($bloguser->user_id);
            echo get_avatar( $user->ID, 46 );
            //...
        }
    }
    

    References: get users of blog function is deprecated and more about the_author_meta.

  2. The the_author_meta( $key); will always retrieve meta information about the author. If you want meta from any user you need to use get_user_meta($user_id, $key, $single); and use $user->ID as id.

    You can find more information in the Codex.