Home | Articles | Wordpress | Showing Popular Posts in WordPress Without Another Plugin

Showing Popular Posts in WordPress Without Another Plugin

For many of us, engaging our readers is what we ultimately want to do, and how readers interact with our site is what we want to know.  If you are a user of WordPress and you actively use the WordPress.com Stats plugin, you already have all the tools you need to display your most popular posts (by page views) for your readers.  Until recently, I have been using the WordPress.com Popular Posts plugin to accomplish this, but I already had everything I needed without using another plugin; and the fewer plugins, the better!

The nice folks over at automattic have opened up there database and have given us an API to retrieve statistical information from WordPress.com’s servers.  Now before you get too carried away, the reason I prefer to use this method utilizing WordPress.com’s Stats plugin (which most everyone I know has installed anyway) over other methods is because it causes no extra load on your site, WordPress.com’s server does all the processing for us.  This means less database calls for our own sites.

… because all of the processing and collection runs on our servers and not yours, it doesn’t cause any additional load on your hosting account. In fact, it’s one of the fastest stats system, hosted or not hosted, that you can use.

We opened our database for developers to retrieve stats. The API is at http://stats.wordpress.com/csv.php and the plugin includes a handy function, stats_get_csv(), which you can use to get your most popular posts. Here is code you can add to your theme based on the work of Ben Gillbanks:

<?php if ( function_exists('stats_get_csv') && $top_posts = stats_get_csv('postviews', 'days=7&amp;limit=8') ) : ?>
 <h3>Currently Hot</h3>
 <ol>
<?<span class="hiddenSpellError" pre="">php foreach ( $top_posts as $p ) : ?>
<ul>
	<li><a href="%3C?%3Cspan%20class=">php echo $p['post_permalink']; ?>"><?php echo $p&#91;'post_title'&#93;; ?></a></li>
</ul>
<?<span class="hiddenSpellError" pre="">php endforeach; ?>
 </ol>
<?<span class="hiddenSpellError" pre="">php endif; ?>

All you have to do is paste the code above in your theme where you would like your most popular posts to be displayed.  You can further customize the date ranges by changing some of the variables within.  For instance, if you would like this to display your most popular posts of all time, simply change the ‘days=7’ to ‘days=-1’

Some of the other things you can modify the code with:

period      String    For use with views table and the 'days' parameter. The desired time period grouping. 'week' or 'month'
                      Use 'days' as the number of results to return (e.g. '&period=week&days=12' to return 12 weeks)
limit       Integer   The maximum number of records to return. Default is 100. "-1" means unlimited.

WordPress.com Stats Plugin

About Joe D

I have always had a passion for everything computing. In early 2000, I decided to take my passion to the web. Thus, C.O.D. was born. Through the years we have made many great friends at C.O.D. and hope to continue our journey for years to come.

Check Also

Manage Multiple WordPress Sites with InfiniteWP

Running your website, or websites, on WordPress is an easy choice as the flexibility the …

3 comments

  1. The stats_get_csv also returns home page, which is not true post and doesn't make sense to show that on list.

    I am doing manual if statement on title to exclude it for now, but any exclude parameter on function ? I can't find it's documentation.

    Your response would help.

    Thanks

    Kaushal

  2. Thanks Joe!

    That finally ended my years of frustration on server loads these popular posts plugins create.

    Your code works perfect over here:
    http://www.2point6billion.com/news

    Thanks to automattic, they are giving one more reason for me to recommend wordpress to my clients.

    Kaushal Subba

    • Glad I could help… now if you want to recommend a web host: http://codwebhosting.com (Computing on Demand web hosting). You can find cheaper hosting easily… but cheaper isn't better.

      Seriously. Every time I investigate a compromised WordPress install, it is either because they were running an old version (usually not just a little bit old, but really old), or because their web host was compromised.

      […]

      When you’re paying $5 a month for hosting, three things will usually suffer: Stability, Security, and Support.

      […]

      Two big priorities right now are: (a) making it super easy to stay up-to-date and (b) pushing web hosts to get their act together.

      http://weblogtoolscollection.com/archives/2011/02

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.