• Resolved mcography

    (@mcography)


    I have a list of users that is currently sorted alphabetically by first name. I need to sort it alphabetically by last name. Does anyone know how I can do this?

    I have this in functions.php:

    /* List Authors Function */
    function knapsack_contributors() {
    global $wpdb;
    
    $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users WHERE ID NOT IN(14) 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);
    echo "</a>";
    echo '<div>';
    echo "<a href=\"".get_bloginfo('url')."/?author=";
    echo $author->ID;
    echo "\">";
    the_author_meta('display_name', $author->ID);
    echo "</a>";
    echo "</div>";
    echo "</li>";
    }
    }

Viewing 1 replies (of 1 total)
  • Thread Starter mcography

    (@mcography)

    Nevermind. I figured it out.

    Here’s the code that worked for me in case anyone else is looking to do the same thing. It orders the users by last name and excludes the admin user (my admin has an ID of 14, you’ll need to change that to the appropriate ID for your admin).

    /* List Authors Function */
    function knapsack_contributors() {
    global $wpdb;
    
    $authors = $wpdb->get_results("
    SELECT user_id
    from $wpdb->usermeta
    WHERE meta_key = 'last_name' and user_id NOT IN(14)
    ORDER BY meta_value");
    
    foreach ($authors as $author ) {
    $user = new WP_User($author->user_id);
    if ($user->has_cap('edit_published_posts')) {
    echo "<li>";
    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>";
    echo '<div>';
    echo "<a href=\"".get_bloginfo('url')."/author/";
    the_author_meta('user_nicename', $author->user_id);
    echo "\">";
    the_author_meta('display_name', $author->user_id);
    the_author_meta('title', $author->user_id);
    echo "</a>";
    echo "</div>";
    echo "</li>";
    }
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Sort Users by Last Name?’ is closed to new replies.