Support » Fixing WordPress » Author list with avatars – how to exclude lower rank users?

  • Resolved kanabi

    (@kanabi)


    Hi,

    I am using below script to display the authors’ names list toghether with their avatars.

    However the script gets all authors, regardless of their rank. Now I’m trying to figure out, how to exclude lower-rank users (subscribers) from the list.

    I’ve found a similar thread here http://wordpress.org/support/topic/display-author-data-exclude-lower-roles but wasn’t able to adapt this solution to my script so far… Any suggestions? Thanks in advance!

    function contributors() {
    global $wpdb;
    $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users WHERE display_name <> 'admin' ORDER BY display_name");
    foreach($authors as $author) {
    echo "";
    echo '<div class="authWrap">';
    echo '<div class="authPic">';
    echo "<a href=\"".get_bloginfo('url')."/?author=";
    echo $author->ID;
    echo "\">";
    echo get_avatar($author->ID,$size = '50');
    echo "";
    echo "</div>";
    echo '<div class="authName">';
    echo "<a href=\"".get_bloginfo('url')."/?author=";
    echo $author->ID;
    echo "\">";
    the_author_meta('display_name', $author->ID);
    echo "";
    echo "</div>";
    echo "</div>";
    echo "";
    }
    }
Viewing 9 replies - 1 through 9 (of 9 total)
  • jnhghy

    (@jnhghy)

    Hi,
    what script are you using to rank users? how is that info saved in the database? in what table?

    kanabi

    (@kanabi)

    I’m using standard WP user roles and settings (WP v. 3.4.1), I guess.

    jnhghy

    (@jnhghy)

    ok, using this

    $user = new WP_User( $author->ID );
    	if ( !empty( $author->roles ) && is_array( $author->roles ) ) {
    		foreach ( $author->roles as $role )
    			echo $role;
    	}

    will give you the user role. so inserting this under:
    foreach($authors as $author) {
    will tell you if the id in use is a low-ranked author or not…
    please verify the variables, I might have errors but these are the functions that you’ll have to use.

    kanabi

    (@kanabi)

    Ok, so now I’ ve got this:

    function contributors() {
    global $wpdb;
    $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users WHERE display_name <> 'admin' ORDER BY display_name");
    foreach($authors as $author) {
    
    $user = new WP_User( $author->ID );
    	if ( !empty( $author->roles ) && is_array( $author->roles ) ) {
    		foreach ( $author->roles as $role )
    			echo $role;
    	}
    
    echo "";
    echo '<div class="authWrap">';
    echo '<div class="authPic">';
    echo "<a href=\"".get_bloginfo('url')."/?author=";
    echo $author->ID;
    echo "\">";
    echo get_avatar($author->ID,$size = '50');
    echo "</a>";
    echo "</div>";
    echo '<div class="authName">';
    echo "<a href=\"".get_bloginfo('url')."/?author=";
    echo $author->ID;
    echo "\">";
    the_author_meta('display_name', $author->ID);
    echo "</a>";
    echo "</div>";
    echo "</div>";
    echo "";
    }
    }

    The script returns no error so far, but still I have no idea how to make it display roles above subscriber only…

    jnhghy

    (@jnhghy)

    ok, let’s try this:

    function contributors() {
    global $wpdb;
    $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users WHERE display_name <> 'admin' ORDER BY display_name");
    foreach($authors as $author) {
    
    $user = new WP_User( $author->ID );
    	if ( !empty( $author->roles ) && is_array( $author->roles ) ) {
    		foreach ( $author->roles as $role )
    			if (($role)&&($role!="subscriber")) { 
    
    echo "";
    echo '<div class="authWrap">';
    echo '<div class="authPic">';
    echo "<a href=\"".get_bloginfo('url')."/?author=";
    echo $author->ID; echo "\">"; echo get_avatar($author->ID,$size = '50');
    echo ""; echo "</div>"; echo '<div class="authName">'; echo "<a href=\"".get_bloginfo('url')."/?author=";
    echo $author->ID; echo "\">"; the_author_meta('display_name', $author->ID);
    echo ""; echo "</div>"; echo "</div>"; echo "";
    } }
                                             }
            }

    let’s see if this wants to work if not well try user_can 😉
    Also consider using a pastebin for so many rows of code!

    kanabi

    (@kanabi)

    Still no error, but the list disappeared completely…

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with get_users() and only include the roles you want. something like this: http://pastebin.com/1ScBmn5Z

    kanabi

    (@kanabi)

    Brilliant, it’s working! Thank you so much:)

    jnhghy

    (@jnhghy)

    happy that is working…

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Author list with avatars – how to exclude lower rank users?’ is closed to new replies.