List users by last name, limit to a specific role, include avatar (8 posts)

  1. Ben Dunkle
    Posted 6 years ago #

    Hi, I'm trying to list users on different pages. They need to sort by last name, A-Z. The list should be limited to users of a specific category, say, contributors, or featured_artist (one of several custom roles I set up with the "user role editor" plugin).
    I've tried a few different plugins, searched dozens of forums, but none seems to do everything I need. I'm guessing my code will look like:

    global $wpdb;
    $users = $wpdb->get_results("INSERT MYSQL VOODOO HERE");
    foreach ($users as $userid) {
    $user = get_firstname($userid);
    $author_posts_url = get_author_posts_url($user->ID);
    echo '<a href="' . $author_posts_url . '">' . ' ' . $avatar. ' ' . $user->user_firstname . ' ' . $user->user_lastname . '</a>';

    Bonus would be if I could get them to spill across 4 vertical columns. Bonus 2 is I design you a custom admin icon for the plugin of your choice if you can help me out!

  2. Ben Dunkle
    Posted 6 years ago #

    OK, I'm almost there. I have

    $roles = array('artist', 'featured_artist');
    /* Loop through users to search for the admin and editor users. */
    foreach( $roles as $role ){
      	// all users with admin or editor role
    	if($role == 'artist' || $role == 'featured_artist') {
    		$this_role = "'[[:<:]]".$role."[[:>:]]'";
    	  	$query = "SELECT * FROM $wpdb->users WHERE ID = ANY (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value RLIKE $this_role) ORDER BY user_nicename ASC LIMIT 10000";
    	  	$users_of_this_role = $wpdb->get_results($query);
    	  	if ($users_of_this_role){
    	    	foreach($users_of_this_role as $user){
    		    	$curuser = get_userdata($user->ID);
    		    	$avatar = get_avatar($user);
    		      	$author_post_url=get_author_posts_url($curuser->ID, $curuser->nicename);
    				echo '<ul class="author">';
    				echo $avatar.'<li><a href="' . $author_post_url . '" title="' .$curuser->display_name.'">' . $curuser->display_name . '</a></li>' . "\n";
    				echo '</ul>';

    and I need to sort that by author lastname.

  3. Joshua David Nelson
    Posted 5 years ago #


    I'm trying to do exactly this. I already figured out pretty much everything you have so far, just trying to get it sorted by lastname. Have you figured that out? Anyone else?


  4. DougJoseph
    Posted 5 years ago #

    The part of the code that says

    ORDER BY user_nicename ASC

    means sort by the field called user_nicename in ascending order.

    If it was changed to ...

    ORDER BY lastname ASC

    ...it might do what you're wanting.

  5. Joshua David Nelson
    Posted 5 years ago #

    thanks for the reply, but that doesn't work either. It turns out it has to do with the users versus usermeta arrays. I actually got it to work with this code:

    function contributors() {
    	global $wpdb;
    	$authors = $wpdb->get_results("SELECT user_id from $wpdb->usermeta WHERE meta_key = 'last_name' ORDER BY meta_value");
    	foreach ($authors as $author ) {
    	$user = new WP_User($author->user_id);
    	if ($user->has_cap('edit_published_posts')) {
    		echo "<div class=\"author_info\"><div class=\"author_pic\">";
    		echo "<a href=\"".get_bloginfo('url')."/author/";
    		the_author_meta('user_nicename', $author->user_id);
    		echo "/\">";
    		echo get_avatar($author->user_id,$size='65');
    		echo "</a></div>";
    		echo "<div class=\"author_title\">";
    		echo "<h3><a href=\"".get_bloginfo('url')."/author/";
    		the_author_meta('user_nicename', $author->user_id);
    		echo "/\">";
    		the_author_meta('display_name', $author->user_id);
    		echo "</h3>";
    		echo "<h4>";
    		the_author_meta('title', $author->user_id);
    		echo "</a></h4></div></div>";
  6. DougJoseph
    Posted 5 years ago #

    I am glad you got it work, and thankful you posted your solution!

    I need to do something similar, so I hope this saves me a few steps.

  7. DougJoseph
    Posted 5 years ago #

    I have decided to use "users.php" instead. I am using the s2Member plugin, and its "users-list.inc.php" affects the normal Users Search (among its other duties). I was able to tweak it so that the normal Users Search now also searches the first_name and last_name in the usermeta table.

    Now my "Users Search" is almost exactly what I need it to be. The last thing I must tackle is getting the Users Search to be sorted (ordered) by usermeta.last_name Anyone know any way to get that done? :-)

  8. justinwhall
    Posted 5 years ago #

    and if one wanted to limit the amounts per page?

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.