Skip to content

Instantly share code, notes, and snippets.

@kitsunde
Created November 24, 2010 15:28
Show Gist options
  • Save kitsunde/713814 to your computer and use it in GitHub Desktop.
Save kitsunde/713814 to your computer and use it in GitHub Desktop.
efficient_wp_list_authors doesn't rape your server: http://core.trac.wordpress.org/ticket/5407
<?php
function efficient_wp_list_authors($args = '') {
global $wpdb;
$defaults = array(
'optioncount' => false, 'exclude_admin' => true,
'show_fullname' => false, 'hide_empty' => true,
'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
'style' => 'list', 'html' => true
);
$r = wp_parse_args( $args, $defaults );
extract($r, EXTR_SKIP);
$exclude_admin = $exclude_admin ? "WHERE user_login <> 'admin'" : '';
$return = '';
/** @todo Move select to get_authors(). */
$users = get_users_of_blog();
$author_ids = array();
foreach ( (array) $users as $user )
$author_ids[] = $user->user_id;
if ( count($author_ids) > 0 ) {
$author_ids = implode(',', $author_ids );
$authors = (array) $wpdb->get_results("
SELECT $wpdb->users.ID, user_nicename, display_name, COUNT($wpdb->posts.ID) AS posts
FROM $wpdb->users " . (!$hide_empty ? 'LEFT' : '') . " JOIN $wpdb->posts
ON $wpdb->posts.post_author = $wpdb->users.ID AND post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . "
$exclude_admin
GROUP BY post_author
ORDER BY display_name");
} else {
$authors = array();
}
if ( empty($authors) ) return false;
if ( $show_fullname ) {
$user_ids = $wpdb->get_col(null);
$link = '';
$meta = (array) $wpdb->get_results("
SELECT user_id, meta_key, meta_value
FROM $wpdb->usermeta
WHERE user_id IN (" . join(',', $user_ids) . ") AND (meta_key = 'first_name' OR meta_key = 'last_name')
ORDER BY meta_key, meta_value, user_id");
$user_meta = array();
foreach ($meta as $v)
$user_meta[$v->user_id][$v->meta_key] = $v->meta_value;
}
foreach ($authors as $author) {
$name = $author->display_name;
if ( $show_fullname && !empty($user_meta[$author->ID]['first_name']) && !empty($user_meta[$author->ID]['last_name']) )
$name = trim($user_meta[$author->ID]['first_name'] . ' ' . $user_meta[$author->ID]['last_name']);
if ( $hide_empty && 0 == $author->posts ) {
$return .= '<li>' . $name . '</li>';
continue;
}
$return .= '<li>';
$link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . sprintf(__("Posts by %s"), attribute_escape($author->display_name)) . '">' . $name . '</a>';
if ( (! empty($feed_image)) || (! empty($feed)) ) {
$link .= ' ';
if (empty($feed_image))
$link .= '(';
$link .= '<a href="' . get_author_rss_link(0, $author->ID, $author->user_nicename) . '"';
if ( !empty($feed) ) {
$title = ' title="' . $feed . '"';
$alt = ' alt="' . $feed . '"';
$name = $feed;
$link .= $title;
}
$link .= '>';
if ( !empty($feed_image) )
$link .= "<img src=\"$feed_image\" border=\"0\"$alt$title" . ' />';
else
$link .= $name;
$link .= '</a>';
if ( empty($feed_image) )
$link .= ')';
}
if ( $optioncount )
$link .= ' ('. $author->posts . ')';
$return .= $link . '</li>';
}
if ( ! $echo )
return $return;
echo $return;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment