Do you use WordPress.Com Stats?
Jan 14, 2009 Articles
I’ve been trying to show my most active posts on my site (for the purposes of driving more traffic) for the past few days, and the plugins that I tried just didn’t seem to work.
That is when I realised I had already installed the WordPress.com Stats plugin. Unfortunately, the plugin is purely for the administration area (consisting of a widget and a full section), so I decided to hack the code to leverage on a plugin I already had.
The Hack
The code I wanted is found inside “stats.php”, where you find the code that displays the admin widgets and page statistics. From there it was just a short search for the function in question (in this case, we need “stats_get_csv()”). Once I figured out what the parameters meant, I was able to integrate it into my page.
The Code
<ul>
<?php
$active_posts = stats_get_csv( 'postviews', 'days=7&limit=5');
foreach($active_posts as $post){
?>
<li>
<?php printf('%s (%s Views)',
'<a href="' . get_permalink( $post['post_id'] ) .
'">' . get_the_title( $post['post_id'] ) .
'</a>', number_format_i18n( $post['views'] ) ); ?>
</li>
<?php } ?>
</ul>
In the code above, “stats_get_csv” simply gets a list of Posts, and we can simply use them to display the information that we want.
Hopefully this might be helpful for anyone out there who is using WordPress.Com Stats, and don’t wish to use another redundant plugin.
Tags: Frontpage, Hack, Most Active, Most Popular, php, Wordpress, Wordpress.com Stats






February 25th, 2010 at 4:02 am
I just came to the same conclusion as you did, as can be seen at http://pepijndevos.nl/2010/02/wordpress-com-stats-top-posts-widget/
I wonder why even the plugins based on WordPress.com Stats have about 100 times the amount of code we have. Maybe something with caching?
Thanks for figuring out the limit=5!
I also discovered something that might be of your interest:
The original snippet includes a piece like this:
This serves to exclude popular posts that have been deleted.
February 25th, 2010 at 6:38 am
Cool. Thanks alot
Though I am personally against deleting posts so this might not be as useful for me. But nevertheless, maybe someone will come by and discover this gem
March 20th, 2010 at 11:28 pm
I think you can do no wrong making things easier for your readers, and I think this is achieved through preview and subscription functionalities.
April 14th, 2010 at 1:35 pm
Here is an extended version of this to create a ‘most popular’ page http://slackermommy.com/programming/wordpress-tips/create-a-most-popular-page-by-extending-wordpress-stats-plugin