WordPress.org

Ready to get started?Download WordPress

Forums

More efficient author list code (8 posts)

  1. bopuc
    Member
    Posted 8 years ago #

    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();
    // --------------------------------------------

  2. lookup
    Member
    Posted 8 years ago #

    Great work!! My modified version:

    function authors_list() {
    $authors = get_authors_info();
    foreach($authors as $author) {
    $cl .= "

  3. ";
    $cl .= 'ID, $author->user_nicename) . '" title="'.wp_specialchars($author->user_nickname) ." �表的文章" . '">';
    $cl .= wp_specialchars($author->user_nickname);
    $cl .= "
     <span class=\"listcount\">" . "(" . $author->user_count . ")" . "</span>";
    $cl .= "
  4. ";
    }
    $authors_list = $cl;
    print $authors_list;
    }

  • moshu
    Member
    Posted 8 years ago #

    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 :)

  • bopuc
    Member
    Posted 8 years ago #

    I did. ;)

  • chuggins
    Member
    Posted 8 years ago #

    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.

  • akudless
    Member
    Posted 7 years ago #

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

  • zefefre
    Member
    Posted 7 years ago #

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

  • zefefre
    Member
    Posted 7 years ago #

    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.

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags