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> <span class=\"listcount\">" . $author->user_count . "</span>";
$cl .= "</li>r";
}
$authors_list = $cl;
print $authors_list;
}
// called with authors_list();
// --------------------------------------------
- The topic ‘More efficient author list code’ is closed to new replies.