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.






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.
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
I think you can do no wrong making things easier for your readers, and I think this is achieved through preview and subscription functionalities.
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
Interesting information, thanks for sharing – I look forward to more.
I have a multi-author blog and I would like to be able to show them their stats/blog entry. Is there a way I can use this function to insert the number of page hits into the post list in the admin section?
Thx.
Modifying the admin section!? That is definitely not recommended.
Unless you’re adding new sections to the admin area, I don’t believe you can actually augment specific parts of existing functionality, and expect it to be forward compatible with future updates.
I’d start investigating the things that you can do with plugins and its limits.
Thanks Daryl. I’ve actually hacked my admin section to death. It might come back to bite me when I go to upgrade, but I figure I will just set up a test region and work through it.
Anyway, let’s say for some odd reason I wanted to put the number of hits that a post has received into the post itself, like on the single.php or I could also put it into my author.php in the article feed there. Got any idea how to make it do that? Thx.
For me, I wouldn’t touch the core files.
What I would do is look for the template files for single posts, and put some code in the template to display the extra information.
I’m not sure if stats_get_csv will do exactly what you want, however. There might be some other function that gives you the number of hits for a single post.
hi all, how can we show that statictics graph in frontpage? It is now stuck in the backend which I think a waste: it can be a great addition to frontpage stat page.
That would be a lot trickier…
at the moment what I’m doing is simply adding extra stuff via php backend commands.
The backend chart is built in Flash, which also means that any data it retrieves is going to be a direct client to admin api call. Probably not possible, I believe.
I tried to put the code in a widget, but I guess it does not work there anyhow.
I would like to display the number of overall visitors at the buttom of my page. Before when my blog was hosted at wordpress.com that supplied an easy widget to to that, but now on my selfhosted blog I fail to somehow display the overall number that can be found under Jetpack / Stats