I want to filter my Author avatar list by by User role

460 Views Asked by At

Here is my code. I only want to display The authors and exclude the rest of the user roles. Please help, I only have a few strands of hair left!!!!

function contributors() { global $wpdb;

$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users ORDER BY display_name");

foreach($authors as $author) 
{ 
echo "< li >"; 
echo "< a href=\"".get_bloginfo('url')."/?author="; 
echo $author->ID; echo "\">"; 
echo get_avatar($author->ID, 125); 
echo ""; 
echo ''; 
echo "< a href=\"".get_bloginfo('url')."/?author="; 
echo $author->ID; 
echo "\">"; the_author_meta('display_name', $author->ID); 
echo ""; 
echo ""; 
echo ""; } }
1

There are 1 best solutions below

7
On

Since I don't know how or where wordpress stores users roles, I suggest to use get_role() like this:

$realUser = wp_get_current_user();
foreach($authors as $author) {
    set_current_user($author->ID);
    if (get_role() != $authorRole) continue;
    set_current_user($realUser->ID);
    // ...
}
set_current_user($realUser->ID);