Wordpress: Author List by Category
I just keep functioning out WP! (What can I say, it’s my job!)
This is a rework of WP’s own list_authors() function, but with some extra SQL:
<?php
function my_list_authors_by_category($args = '', $cat_id=0) {
global $wpdb;
$defaults = array(
'optioncount' => false, 'exclude_admin' => true,
'show_fullname' => false, 'hide_empty' => true,
'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true
);
$r = wp_parse_args( $args, $defaults );
extract($r, EXTR_SKIP);
$return = '';
/** @todo Move select to get_authors(). */
// $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name");
//replace with the 'author by category' SQL
$authors = $wpdb->get_results("SELECT distinct post_author as `ID`, user_nicename FROM `default_posts` as `dp`, `default_terms` as `dt`, `default_term_relationships` as `dtr`,`default_users` as `du` where `dp`.`post_author`=`du`.`ID` and `dp`.`ID`=`dtr`.`object_id` and `dtr`.`term_taxonomy_id`=".$cat_id.";");
$author_count = array();
foreach ((array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row) {
$author_count[$row->post_author] = $row->count;
}
foreach ( (array) $authors as $author ) {
$author = get_userdata( $author->ID );
$posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
$name = $author->display_name;
if ( $show_fullname && ($author->first_name != ” && $author->last_name != ”) )
$name = "$author->first_name $author->last_name";
if ( !($posts == 0 && $hide_empty) )
$return .= ‘<li>’;
if ( $posts == 0 ) {
if ( !$hide_empty )
$link = $name;
} else {
$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\" style=\"border: none;\"$alt$title" . ‘ />’;
else
$link .= $name;
$link .= ‘</a>’;
if ( empty($feed_image) )
$link .= ‘)’;
}
if ( $optioncount )
$link .= ‘ (’. $posts . ‘)’;
}
if ( !($posts == 0 && $hide_empty) )
$return .= $link . ‘</li>’;
}
if ( !$echo )
return $return;
echo $return;
}
?>
And then to call, just define the category you want:
<?php
$cat=0;
if (have_posts()){ //get the present category, if there is one
if (is_category()) {
$cat = get_query_var('cat');
}
}
if($cat!=0){
?>
<h2>Authors:</h2>
<ul>
<? my_list_authors_by_category('exclude_admin=1',$cat); ?>
</ul>
<? } ?>