WordPress.org

Support

Support » Plugins and Hacks » [Resolved] How do you display author’s post count and comment count?

[Resolved] How do you display author’s post count and comment count?

  • Hi, I was just wondering, how do you display author’s post count and comment count? I want them to display in singlepost.php. Please, what is the code that I need to use. Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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>';
    }
    ?>

    Thanks but I want it for individual users. Like in their mini bio below every post they made.

    Okay, this need to be in a post loop or you need change $user_id value

    <?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>';
    ?>

    Thank You. It worked.

    The code given for all authors doesnt seem to work, its only showing 2 of 3 arthors.

    Thanks for providing this code! The $wpdb functions are bewildering to me, even though I know a little bit about SQL queries.

    I am trying to use a variation of the code you posted here, MichaelH, to get a count of the number of “pending” posts the current user has. Only thing is, it’s not working. Can you see where I’m going wrong here?

    global $wpdb;
    $where = "WHERE post_status = 'pending' AND post_author = " . $user_id;
    $post_count = $wpdb->get_var(
    "SELECT COUNT (*) FROM $wpdb->posts $where");
    $welcomemessage .= "<p>You currently have $post_count submissions pending.</p>";

    Moving my question to a new topic.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] How do you display author’s post count and comment count?’ is closed to new replies.
Skip to toolbar