Do you use Wordpress.Com Stats?

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.

Share this post: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Facebook
  • Google Bookmarks

3 Responses to “Do you use Wordpress.Com Stats?”

  1. 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:

    1
    if(!get_post($post['post_id'])) continue;

    This serves to exclude popular posts that have been deleted.

  2. 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 :)

  3. Thx for this nice article.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>