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.