WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Author list with avatars - how to exclude lower rank users? (10 posts)

  1. kanabi
    Member
    Posted 2 years ago #

    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 "";
    }
    }
  2. jnhghy
    Member
    Posted 2 years ago #

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

  3. kanabi
    Member
    Posted 2 years ago #

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

  4. jnhghy
    Member
    Posted 2 years ago #

    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.

  5. kanabi
    Member
    Posted 2 years ago #

    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...

  6. jnhghy
    Member
    Posted 2 years ago #

    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!

  7. kanabi
    Member
    Posted 2 years ago #

    Still no error, but the list disappeared completely...

  8. keesiemeijer
    moderator
    Posted 2 years ago #

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

  9. kanabi
    Member
    Posted 2 years ago #

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

  10. jnhghy
    Member
    Posted 2 years ago #

    happy that is working...

Topic Closed

This topic has been closed to new replies.

About this Topic