4

I want to have a page listing all the authors and the number of posts each of them has published. Is there a plugin that does this already? Being able to count number of comments as well would be a plus.

Thanks for the help.

3

I don't know about a plugin but you can use this:

In author.php you can use this to count posts:

<?php
global $wp_query;
$curauth = $wp_query->get_queried_object();
$post_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'post' AND post_status = 'publish'");
?>
<h2>Post Count: <?php echo $post_count; ?></h2>

Inside a Loop you can use this to count posts and comments:

<?php
global $wpdb;
$user_id = $post->post_author;  //change this if not in a std post loop
$where = 'WHERE comment_approved = 1 AND user_id = ' . $user_id ;
$comment_count = $wpdb->get_var(
    "SELECT COUNT( * ) AS total
        FROM {$wpdb->comments}
        {$where}
    ");
$user = get_userdata($user_id);
$post_count = get_usernumposts($user->ID);
echo '<p>User ' . $user->display_name . ' post count is ' . $post_count .', comment count is ' . $comment_count . '</p>';
?>

For All Authors:

<?php
global $wpdb;
$where = 'WHERE comment_approved = 1 AND user_id <> 0';
$comment_counts = (array) $wpdb->get_results("
        SELECT user_id, COUNT( * ) AS total
        FROM {$wpdb->comments}
        {$where}
        GROUP BY user_id
    ", object);
foreach ( $comment_counts as $count ) {
  $user = get_userdata($count->user_id);
  $post_count = get_usernumposts($user->ID);
  echo '<p>User ' . $user->display_name . ' post count is ' . $post_count .', comment count is ' . $count->total . '</p>';
}
?>
| improve this answer | |
  • Thanks, Philip. I tried your first and second snippets and I made author.php display number of posts and number of comments. However, I couldn't get the third snippet to work. I created a page template and put your code in it, but it didn't work. I guess it doesn't work in a page template. I actually installed "Member List" plug-in and I wanted to use your code to display # of posts and comments in the member list page, but I couldn't get it to work either. PHP looks like Latin to me. Anyway, thanks for the answer. I will wait a bit more, if nobody knows plugins, I will mark your answer. – evergreen Apr 1 '11 at 0:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.