I have the following code and it lists the users by ID, I need that changed to be by last name (preferably) or nice name.
<?php
// Displays displays all users regardless of if they have a post.
$blogusers = get_users_of_blog();
if ($blogusers)
{
foreach ($blogusers as $bloguser)
{
$args = array(
'author' => $bloguser->user_id,
'showposts' => 1,
'caller_get_posts' => 1
);
$my_query = new WP_Query($args);
$user = get_userdata($bloguser->user_id);
$out='';
$out.='<div class="about_the_employee">';
$out.='<div class="employee_info">';
$out.='<img src="http://www.gravatar.com/avatar/' . md5( strtolower( trim( $user->user_email) ) ).'?s=90" alt="'. $user->display_name .'" class="avatar" />';
$out.='<div class="employee_desc">';
$out.='<h3>'. $user->display_name .'</h3>';
$out.=$user->description;
if(isset($user->twitter) || isset($user->user_url))
{
$out.='<div class="employee_links">';
if(isset($user->user_url) && $user->user_url!='')
{
$out.='<span><a href="'. $user->user_url .'" title="Website">My Website</a></span>';
}
if(isset($user->twitter))
{
$out.='<span><a href="http://twitter.com/'. $user->twitter .'" title="Follow '. $user->display_name .' on Twitter" class="twitter_link">@'. $user->twitter .'</a></span> ';
}
$out.='<div class="clear"></div>';
$out.='</div>';
}
$out.='</div>';
$out.='<div class="clear"></div>';
$out.='</div>';
$out.='</div>';
echo($out);
}
}
?>
Ivaylo Draganov answers:
Hello,
try using the function <em>get_users</em> with parameter <em>orderby</em>:
$blogusers = get_users(array(
'orderby' => 'nicename'
)
);
There's more info on this function in the Codex:
[[LINK href="http://codex.wordpress.org/Function_Reference/get_users"]]http://codex.wordpress.org/Function_Reference/get_users[[/LINK]]
I'm not sure whether it can order by last name. The documentation states that:
<blockquote>orderby - Sort by 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count'</blockquote>
And don't use the fucntion <em>get_users_of_blog()</em> as it has been deprecated.
Jake Caputo comments:
I was aware that get_users_of_blog was depreciated, but if I change it to get_users it completely breaks the code, so I was looking for a quick fix without completely redoing the code. =/
So anyway, your suggestion does not work. It just displays users with no information and blank gravatars.
Ivaylo Draganov comments:
It's intended to work :) But there has been a slight change in the PHP object. So now uou have to get the user ID like that:
$user = get_userdata($bloguser->ID);
I did a quick test and it's working on my setup.
Jake Caputo comments:
Awesome. That did it. Thanks.
Maor Barazany answers:
The function [[LINK href="http://codex.wordpress.org/Function_Reference/get_users_of_blog"]]get_users_of_blog[[/LINK]] is deprecated. You should use [[LINK href="http://codex.wordpress.org/Function_Reference/get_users"]]get_users [[/LINK]]instead.
The get_users have also a <strong>orderby</strong> parameter. You can use -
<?php get_users('orderby=display_name'); ?>
or -
<?php get_users('orderby=nicename'); ?>
Jake Caputo comments:
See the reply to the post above you;
if I change it to get_users it completely breaks the code, so I was looking for a quick fix without completely redoing the code. =/
get_users does not work.