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 } ?>

How was it for you? Let me know on BlueSky or Mastodon

(Please) Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

17 Oct 2012

WordPress Social Network Aggregation

I really like the idea of a Tumblog – and even have one on Tumblr.com – but I don’t promote it anywhere. Conceptually it’s great – but I don’t like not having control over my content.What I would really like...
19 Nov 2012

Jetpack: The Ultimate WordPress Plugin?

I’ve now got 2 themes on WordPress.com – Mimbo Pro and Opti – and as such I have spent quite a lot of time on the site. WordPress.com is powered by WordPress – but it also has a lot of...
14 May 2013

Redesigning the WordPress Post Editor

Ghost is a project born from frustration with WordPress. Ironically it seems to be mostly WordPress power users who want to use it. The Ghost team – led by John O’Nolan – put Ghost on KickStarter last week and it...
20 Feb 2013

The Amazing Art of Flipping Websites

Recently I have looked at expanding my internet empire. I have all sorts of ideas and never enough time – so I thought I would see if I could buy some websites relatively cheaply, improve them, and then either flip...