WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Display author data (exclude lower roles)

[Resolved] Display author data (exclude lower roles)

  • I want to create a list of the authors in my multi-author blog, and display the number of posts they published this month and the number of comments on those posts.

    I was able to get the following:

    <table>
    <tr><th>User</th><th>Post Count</th></tr>
    <?php
    //displays users along with count of posts each user has belonging to current month
    $current_month = date('m');
    $current_year = date('Y');
    $blogusers = get_users_of_blog();
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $user = get_userdata($bloguser->user_id);
        $userposts = get_posts('year=' . $current_year . '&monthnum=' . $current_month . '&author=' . $bloguser->user_id);
        $count=count($userposts);
        echo '<tr><td>User ID ' . $user->ID . ' ' . $user->display_name . '</td><td> '. $count .'</td></tr>';
      }
    }
    ?>
    </table>

    However, I can’t figure out how to exclude non-authors (like the “subscriber” role) from this list. I also need some help aggregating the number of comments on those posts to add to another table column. Any thoughts? Thanks in advance!

Viewing 10 replies - 1 through 10 (of 10 total)
  • I can’t be sure, but I think this will work:

    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $user = get_userdata($bloguser->user_id);
        if ($user->user_level < 2) continue;
        $userposts = get_posts('year=' . $current_year . '&monthnum=' . $current_month . '&author=' . $bloguser->user_id);
        $count=count($userposts);
        $running_total += $count;
        echo '<tr><td>User ID ' . $user->ID . ' ' . $user->display_name . '</td><td> '. $count .'</td><td>' . $running_total . '</td></tr>';
      }
    }

    That is excellent! Is there any way to apply a class to the table cell that has the most posts, or the most comments?

    This will set the name cell to class=’maxcount’ for the row(s) with the most posts:

    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $user = get_userdata($bloguser->user_id);
        if ($user->user_level < 2) continue;
        $userposts = get_posts('year=' . $current_year . '&monthnum=' . $current_month . '&author=' . $bloguser->user_id);
        $count=count($userposts);
        $running_total += $count;
        $rows[] = array('id' => $user->ID,'name' => $user->display_name,'count' => $count,'total' => $running_total);
        if ($count > $maxcount) $maxcount = $count;
      }
      foreach ($rows as $row) {
        $class = ($row['count'] == $maxcount) ? " class='maxcount'" : '';
        echo "<tr><td$class>User ID " . $row['id'] . ' ' . $row['name'] . '</td><td> '. $row['count'] .'</td><td>' . $row['total'] . '</td></tr>';
      }
    }

    I’m realizing that this is missing the comment count. The running total is counting the total number of posts in the list, rather than counting the number of comments.

    I’m looking to count the total number of comments on each author’s posts this month. Any ideas?

    I think it can be done, but will probably add a great deal of overhead to the script. As far as I can tell, it would require a loop inside the ‘foreach ($blogusers …’ loop to get the comment count for each post. Loops inside loops are generally something to avoid.

    Do you still want to proceed?

    If you do, I am not clear as to whether you want the total number of comments on the posts, or just the comments by the author.

    I do!

    I want the total number of comments on the posts that each author wrote in that particular month.

    So if Sally wrote 3 posts in October, I want to calculate how many comments were on those posts (total).

    Is there a way to add a separate loop to calculate this? Essentially it would be: for this user, count the number of comments on the posts that they wrote that were published this month.

    OK, give this a try:

    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $user = get_userdata($bloguser->user_id);
        if ($user->user_level < 2) continue;
        $userposts = get_posts('year=' . $current_year . '&monthnum=' . $current_month . '&author=' . $bloguser->user_id);
        $count=count($userposts);
        $running_total += $count;
        $comment_count = 0;
        foreach ($userposts as $upost) {
          $comments_by_type = &separate_comments(get_comments('post_id=' . $upost->ID));
    	   $comment_count += count($comments_by_type['comment']);
        }
        $rows[] = array('id' => $user->ID,'name' => $user->display_name,'count' => $count,'total' => $running_total,
          'comment_count' => $comment_count);
        if ($count > $maxcount) $maxcount = $count;
      }
      foreach ($rows as $row) {
        $class = ($row['count'] == $maxcount) ? " class='maxcount'" : '';
        echo "<tr><td$class>User ID " . $row['id'] . ' ' . $row['name'] . '</td><td> '. $row['count'] .'</td><td>' . $row['total'] .
           '</td><td>' . $row['comment_count'] . '</td</tr>';
      }

    That’s it! Thank you SO MUCH!

    Glad its working! Now, please use the dropdown at top right to mark this topic ‘Resolved’ so that anyone researching a similar problem can see that there is a solution.

    I need this exact same thing that vtxyzzy suggested, but the get_users_of_blog is deprecated with WordPress 3.1 and I get an error when trying with just get users of Parse error: syntax error, unexpected T_ENDWHILE in file.php on line 56.

    This was the code I am attempting to use that gets this error:

    [Code moderated as per the Forum Rules. Please use the pastebin]

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Resolved] Display author data (exclude lower roles)’ is closed to new replies.
Skip to toolbar