Support » Plugins and Hacks » Sort authors by last name

  • A question that has come up a lot on the support forums is how to sort list_authors(). In my case, I needed to sort a multi-author blog by last name and return the results last name first, i.e…

    LastnameA, FirstnameA
    LastnameB, FirstnameB
    LastnameC, FirstnameC

    …all while maintaining links to the authors’ archive pages.

    Respondents said you’d have to re-write a chunk of template-functions-authors.php, which a friend of mine went ahead and did for my site. While I can’t vouch for how foolproof this code is–we’re not pros by any stretch–it does work for my site in the intended way. (In any case, back up template-functions-authors.php before proceeding.)

    Basically the hack is the following. Go to line 179 or thereabouts in template-functions-authors.php. Replace the entire line that starts with $query and replace it with this:

    $query = "select ID, usermeta_ln.meta_value as last_name, usermeta_fn.meta_value as first_name from wp_usermeta as usermeta_ln, wp_usermeta as usermeta_fn, wp_users where usermeta_ln.meta_key = 'last_name' and usermeta_ln.user_id = ID and usermeta_fn.meta_key = 'first_name' and usermeta_fn.user_id = ID order by last_name";

    Doing so will collect all the relevant data and sort it by last_name.

    Then, a few lines down, replace everything from $name through the first if-statement with this:

    $name = $author->last_name + ', ' + $author->first_name;
    if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
    $name = "$author->last_name, $author->first_name";

    This will generate a “Lastname, Firstname” list when list_authors() is set to return your authors’ full names.

    My hunch is that there was a bit of dumb luck in this hack, and thus someone could come along and clean this up. If so, please do.

Viewing 2 replies - 1 through 2 (of 2 total)
  • jsievikorte


    Didn’t work for me that way. Quick check through my php reference and came up with following line for the first $name entry.

    $name = $author->last_name.", ".$author->first_name;

    akwhitacre: Wow, I’ve been wanting to do this for months! Thank you!

    See Roster page now sorted by last name, but also in sidebar “Member Postings” sorted by last name and in the format LastName, FirstName!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sort authors by last name’ is closed to new replies.