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:
<?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
To get a specific user’s meta using
the_author_meta()
function you need to pass the user id, i.e.So in your code you can use
To exclude some users you can check user ids using
in_array
function, i.e.References: get users of blog function is deprecated and more about the_author_meta.
The
the_author_meta( $key);
will always retrieve meta information about the author. If you want meta from any user you need to useget_user_meta($user_id, $key, $single);
and use$user->ID
as id.You can find more information in the Codex.
Try it
see the URL
https://wordpress.stackexchange.com/questions/10091/author-custom-fields-post-meta-the-code