Post Pic

How to: Display your average comments per posts

Do your blog gets a lot of comments? Here is an easy way to know the average comments per posts on your WordPress blog, using new functions introduced in WordPress 2.7.

Simply paste the following code anywhere on your theme, where you'd like the average comments per post to be displayed:

<?php
$count_posts = wp_count_posts();
$posts = $count_posts->publish;

$count_comments = get_comment_count();
$comments  = $count_comments['approved'];

echo "Average ".round($comments/$posts)." comments per post.";
?>

This code is quite simple: First, we get the total number of published posts and the total number of approved comments. Once we have it, we just have to divide the number of comments by the number of posts.

19 Responses

Feb 09 2009 19:46

That’s a really cool recipe!

Feb 09 2009 23:28

Heh, simple math mixed with some php. Nice one.

Feb 11 2009 02:02

Great ! Thx !

Feb 14 2009 14:44

This recipe makes me sick of how inconsistently named Wordpress API is. As a programmer, who has had to enhance existing Wordpress site, I can say that Wordpress API is pretty incomprehensible…

Aug 05 2009 09:02

I find that this worked quite well when I placed it in the sidebar, but it resulted in the blog post not showing up on the page.

Aug 16 2009 17:17

How to display this information on the page?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required