Support » Plugins and Hacks » More efficient author list code

  • The built-in “list_authors” and “wp_list_authors” (which uses “list_authors”) is a resource sucking nightmare if you turn on the post count feature. A list of 30 authors with varying post counts from 1 to 489 was causing 134 hits to the database *on every page load*. On a high volume site, this was killing the server. Not to mention the page generation time was up to 15 seconds.

    So, I wrote my own. One single query to the database and page rendering is back down to a second. 🙂
    Unfortunately, I don’t have the time to package it up and host a plugin, but it was suggested I drop the code in here for others to use and adapt as they see fit. I hope that’s ok? :\


    // --------------------------------------------
    // AUTHORS LISTING, with entry counts
    // --------------------------------------------
    //
    function get_authors_info() {
    global $wpdb;
    $query = "SELECT DISTINCT
    U.ID AS user_id, user_nickname, user_firstname, user_lastname, user_nicename,
    COUNT(P.ID) AS user_count
    FROM
    $wpdb->users AS U,
    $wpdb->posts AS P
    WHERE
    user_login <> 'admin'
    AND P.post_author = U.ID
    AND P.post_status = 'publish'
    GROUP BY
    U.ID
    ORDER BY
    user_firstname
    ";
    $authors = $wpdb->get_results($query);
    return $authors;
    }
    function authors_list() {
    $authors = get_authors_info();
    foreach($authors as $author) {
    $cl .= "t<li>";
    $cl .= '<a href="' . get_author_link(0, $author->ID, $author->user_nicename) . '" title="Posts by '.wp_specialchars($author->user_nickname) . '">';
    $cl .= $author->user_firstname . " " . $author->user_lastname;
    $cl .= "</a>&nbsp;<span class=\"listcount\">" . $author->user_count . "</span>";
    $cl .= "</li>r";
    }
    $authors_list = $cl;
    print $authors_list;
    }
    // called with authors_list();
    // --------------------------------------------

Viewing 7 replies - 1 through 7 (of 7 total)

If you put the code in between backticks (check out the instructions below the text input area!) others might be able to use your code 🙂

I did. 😉

Maybe this is a stupid question: But how would one incorporate this code into their wordpress blog (i.e., step by step)? I’m a pretty basic user: I can install plugins, and copy and paste into a template, but I’m not quite sure what I would do with this code.

I’m new like chuggins above. Could someone help out with more details instructions on how to use this code?
Thanks

Does anyone has any idea how to list authors based on their post count?

Oh I just figured it out, thank you for the code!

@chuggins & akudless
You should add the code to template-functions-author.php and call the function from your template/theme file. The code need some modification though to make it works with latest version of wordpress.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘More efficient author list code’ is closed to new replies.