WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How do you display author's post count and comment count? (8 posts)

  1. Armin100
    Member
    Posted 5 years ago #

    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.

  2. MichaelH
    Member
    Posted 5 years ago #

    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>';
    }
    ?>
  3. Armin100
    Member
    Posted 5 years ago #

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

  4. MichaelH
    Member
    Posted 5 years ago #

    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>';
    ?>
  5. Armin100
    Member
    Posted 5 years ago #

    Thank You. It worked.

  6. wpchoice
    Member
    Posted 4 years ago #

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

  7. dtweney
    Member
    Posted 4 years ago #

    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>";
  8. dtweney
    Member
    Posted 4 years ago #

    Moving my question to a new topic.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags