Home/ Journal Bens Projects Binary Moon Archives About Ben Gillbanks Subscribe to Binary Moon Updates

Subscribe to Binary Moon Website Updates, it's Free and Easy to Stay in Touch

Search Binary Moon

Search Binary Moon

Quick and Easy Popular Posts for Your WordPress Blog – in Only 1 Line of Code

Adding a list of popular posts to your website is a great way to increase user engagement on your site. It shows your readers what people are reading and interacting with the most - which in turn should encourage people to browse around your website more.

There are lots of posts across the blogosphere describing how to do this. Some look at the number of comments, and others track the page views, but they are all quite complex and cumbersome.

I even developed a system for looking at the most popular posts based upon comment count, with a date limit to keep things recent, and it worked (you can still see it in action on the homepage carousel), but wasn't perfect.

Then today I had a brain(fart)wave.

I have used the WordPress stats plugin for a while and it displays a nice little list of the most popular posts on the WordPress Dashboard... and I got to thinking - "why don't I use that list myself?"

My first stop was the plugin code - and 5 minutes later I had found a very simple couple of lines of code that would grab the most popular posts using the stats api.

5 minutes after that I had written the following code, and added it to the #footer of this very site (it's in the right hand column labelled "Currently Hot").

<?php
	if (function_exists('stats_get_csv')) {
		$top_posts = stats_get_csv ('postviews', 'days=7&limit=8');

		if (count($top_posts) > 0) {
			echo '<ol>';
			foreach ($top_posts as $p) {
?>
				<li><a href="<?php echo $p['post_permalink']; ?>"><?php echo $p['post_title']; ?></a></li>
<?php
			}
			echo '</ol>';
		}

	}
?>

This code works really well, the stats are accurate, and it even has built in database caching. What's not to like?

Note: The WordPress stats plugin, fully enabled, is a requirement for this to work.

Oh - and if you want the 1 line version check below. Personally I prefer the slightly longer version above.

<?php foreach ($top_posts = stats_get_csv ('postviews', 'days=7&limit=8') as $p) { ?><a href="<?php echo $p['post_permalink']; ?>"><?php echo $p['post_title']; ?></a><?php } ?>

31 Responses to “Quick and Easy Popular Posts for Your WordPress Blog – in Only 1 Line of Code” Leave a reply ›

  • I'm not a fan of WP stats plugin, it is worth using ?

    • I quite like it myself. It's very simple, and I use Google Analytics as well, but as a simple way to get some quick numbers it's a cool thing to add.

  • This is a really cool idea. I had been using the Popularity Contest plugin but this one seems so simple as it displays data already used by the WordPress.com Stats plugin.

  • I like this a lot. Excellent idea.

    Now if only there was a way to have this be part of the official plugin... i don't like themes having to rely on a plugin.

    Excellent stuff as always Ben.

  • Great tip! Is there an argument for stats_get_csv to exclude specific page/post IDs from the results? I tried "exclude=", but that didn't seem to work...

    • There's no documentation on the function but my guess is no. The query gets passed to a wordpress.com site so it's not as flexible as the query_posts command (for example).

      If you print_r the value of p in the foreach loop then you can see the post/ page ids so there's no reason you couldn't add in an if statement or two to exclude the posts you want to hide.

  • I kluged together a widget for the folks who want to put this in their sidebar:

    http://github.com/boogah/Trendy

    Thanks for the code!

  • Hey Ben, not strictly related with this post....but just downloaded ur Regulus theme for my WP blog. Thanks, really sweet design. Would like a bit more functionality however, such as the addition of a navigation menu for the pages...or a pull down menu when the page titles are hovered over - if you know what I mean. As I am new to this WP gig...what would your suggestions be in order for me to do that? Can you help?

  • Hey,

    Good stuff!

    It seems I'm not the only one who figured this out.
    My post: http://pepijndevos.nl/...ess-com-stats-top-posts-widget/
    Another post I found: http://darylteo.com/...14/do-you-use-wordpresscom-stats/ << he was apparently first.

    The plugin FAQ is linking here by the way...

  • Hi.. Ben.. Awesome stuff!
    I just want to know, how to display my TOTAL stats just like blog at wordpress.com in sidebar? In another case, for example to show off my Akismet stats, I just put in my sidebar template. So, If I want to show off my stats, what should i do?

    Thanks!
    *sorry for my bad english.. I'm from Indonesia :)

  • I have used your code to create my own "most popular" page - http://slackermommy.com/...ending-wordpress-stats-plugin

    Thanks again for the code

  • Nice work.
    I wonder if it is possible to filter the shown posts by categories for instance.
    Thanks for the help.

  • Hey Ben, do you know if it's possible to replace the post title with the new post thumbnail feature? I've been searching for days to find a way to sort the post and display them as thumbnails for days, but nothing seems to be working. Thanks for any help.

  • Hi there, firstly thanks fort this, very useful. Do you have any idea how to remove the home page from the outputted stats though, for me (and most id assume) the home page is going to always be the most popular, and it would be good to not have this added in the mix.

    Thanks in advance.

    • I actually added this myself after I wrote the article :)

      It's quite easy to do. Just add a conditional, inside the foreach loop that checks if the post_id is > than 0. Something like:

      if ($p['post_id'] > 0) {
      • Hi Ben, thanks for the response. I can not for life of me get this working, I just get the blankscreen or no change. I will continue to bang my head against the wall on this until I get it fixed (I hope). Problem lies obviously with my misunderstanding of conditional tags and how to properly implement them, im a total trial and error donk with this stuff, but this one isnt coming as easy as the rest.

        If you could see your way as to actually spell it out for me, I would be evry grateful. Its certianly not working above the below echo, and I can find a way to get it working in the above loop, I figured I need to go if then else to let it move on to the next if statment but nope.

        -(

  • Thanks for coming up with this & sharing it. I was looking at Popularity contest for most pop, but think I will use this since I have stats already installed. I was able to filter out the home page, thanks for that as well (for Phil, the conditional goes after the foreach statement but before the opening curly bracket).

    Two things I noticed though, and was puzzled by: I changed the 'limit' statement at the beginning to 11 from 8 to get a net of top ten items, but it did not come through for some reason (after taking out home page, I got a net of seven). Need to do something different? Also I was wondering how to filter out pages, and show only posts.

    Above all, thanks again for sharing this snippet, its a huge help for me.

  • Hi Ben,

    Any chance you can provide updated code with the conditional inserted to remove the homepage for us folks who are not PHP programmers?

    Thanks,
    Alan

  • Great! But please share the code to remove the homepage in the code, this make it much better. Otherwise it looks unprofessional and maybe not worth the extra loading time for the WordPress Stats plugin in admin cpanel.

  • Creative idea Ben.. And thanks for your effort. I sure will use this idea on my blog

Leave a Reply

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

About me

About BenMy name is Ben Gillbanks. I'm a lover of Video Games, WordPress, Web Development and everything in between.

I have been working on the internet since 1998, and working with computers even longer. I am a hardcore Nintendo fanboy and have owned most of their consoles at one stage or another.

Read more about me on my about page.


Follow Me


Random Link-outs

The Binary Network links to all my websites
bengillbanks.co.uk - Ben Gillbanks

Ben Gillbanks

All my websites under 1 roof, the easiest way to find out what I do

Pro Theme Design - premium WordPress themes

Pro Theme Design

Premium WordPress themes by web design pros (erm... that includes me)

Binary Joy - gaming news and reviews

Binary Joy

Gaming news and reviews

Binary Sun - play free online games

Binary Sun

Play and download free and paid games (many made by me)

Gaming Angel - download and play games online

Gaming Angel

Stacks of shareware games, free to try and cheap to buy