With the popularity of WordPress as a blogging platform, there is certainly no shortage of plugins available to help blog owners tweak their site to do amazing things (and sometimes not so amazing things).
One of the plugins that is quite popular – no pun intended – is the Recently Popular plugin that provides a widget to display the most popular posts over a specified period of time. This is a plugin that I have used myself on a few blogs and was about to use on this blog…
And then it hit me.
Rather than installing another plugin to accomplish the goal of adding the most popular posts to the sidebar, why not see if I can leverage an existing plugin that displays blog stats within the administration panel?
Content So Hot You’ll Freak
Sure, I could have gone with the more standard “Recently Popular” or “Popular Posts” descriptions but that’s not my steeze. Instead, you will now see a listing in the sidebar that contains the top ten posts over the last 90 days – more appropriately named “Content So Hot You’ll Freak”.
How can you add this to your blog?
First, you will need to be sure that you are running the WordPress.com Stats plugin that was mentioned earlier. In addition, you will need to be running one of the plugins that allow you to execute PHP code in your widgets or posts (my plugin of choice for this is Exec PHP).
Once you have satisfied the pre-requisites, you simply need to add the following code to one of your sidebar widgets:
1 2 3 4 5 6 7 8 9 | <?php if ( function_exists('stats_get_csv') && $top_posts = stats_get_csv('postviews', 'days=90&limit=11') ) : ?> <ul> <?php foreach ( $top_posts as $p ) : ?> <?php if($p['post_id'] != 0) : ?> <li><a title="<?php echo $p['post_title']; ?>" href="<?php echo $p['post_permalink']; ?>"><?php echo $p['post_title']; ?></a></li> <?php endif; ?> <?php endforeach; ?> </ul> <?php endif; ?> |
But I don’t want my home page in the list…
The bulk of the code snippet above is directly from the FAQ for the WordPress.com Stats plugin, but the problem was that the blog home page was displaying as one of the top 10 results.
In order to prevent the home page from displaying in the list, I added the following conditional statement within the loop that writes out each link:
1 2 3 | <?php if($p['post_id'] != 0) : ?> ... <?php endif; ?> |
Basically, this is simply saying that I only want to display the link if the blog post id is anything other than zero as the blog home page has an id of zero.
Brilliant, don’t you think?
How to Customize the Results
As I mentioned earlier, I decided to show the top 10 posts over the last 90 day period but maybe you want to display the top 5 results over the last 7 days, or maybe you would prefer the top 18 posts over the last 22 days.
Whatever the criteria is that you want to use, the piece of code that you need to pay attention to is:
1 | 'days=90&limit=11' |
All you need to do is change the values for the “days” parameter and/or the “limit” parameter to get your desired result.
One important note, you will notice that I have a limit of 11 yet I am only displaying 10 posts. Well, if you remember that we filtered out the blog home page from being displayed, we need to account for that by retrieving one more post than we actually want to display.
Yes, this means it is entirely possible that we will end up displaying 11 posts if the blog home page is not among the top 10 visited posts, but that is most likely not going to happen as the home page is often one of the most visited pages of any site.
So now that you know how I have added the “Content So Hot You’ll Freak” to my sidebar, take a minute to check out the most visited posts and let me know which one is your favorite!






{ 27 comments… read them below or add one }
Interesting approach, Derek!
I was wondering, though, if there’s extra security concerns to allow php to run inside a post. (I am asking because I don’t know as much in this aspect.)
.-= Kelvin Kao hopes you will read… What You Can Learn From Popeye The Video Game =-.
Hi Kelvin, thanks for the comment.
From the security perspective, the primary risk is that you’re giving the author of the post / widget the ability to input (and execute) any php code within the framework of the site. Since there are no restrictions on what code can be written, this could be quite damaging.
There is also an issue in the case of a blog with multiple authors, as it is possible to have a situation where one author may have permission to write php code and can add it into posts from another author that does not have permission.
Assuming that the person adding the php code to the post has a good understanding of what they are doing, there really isn’t much more risk than a post without php – as the underlying code that displays the post is generated using php anyways.
So, as a summary to my long-winded response, I’d say that the primary security concern is having the knowledge as the author not to input code that could crash the site or cause other problems. If your blog grants accounts to guest authors, you’ll also need to be sure you restrict their ability to add php code if you don’t feel comfortable allowing them that level of control.
Hope that makes sense.
I was wondering the same thing about PHP – I guess you just have to find people you can trust.
Great post… Interesting one. Thanks for the idea.
.-= ipad skins hopes you will read… Apple ipad Skins =-.
Where can I change post limits ?
On the first line of the code, change the number after the text “limit=” from the current value of 11 in my example to whatever number you would like to display. As a reminder, my example has the limit set to one more than we want to display as we filter out the home page from the results.
Hope that helps.
had a good time reading this one..
I can tell just by looking at this blog that you know what you’re doing! Fantastic article and if I can ever figure out how to start using wordpress I’ll take advantage of that code!
Ben
Ben, thanks for the comment. Please feel free to ask any WordPress questions and I will do my best to help you out.
Hi Derek,
Not been around here for a while but this post really caught my attention as I have been looking for a better way to incorporate other parts of the core WP options into my blog without actually installing new plugins as my blog already seems pretty resource hungry.
Will try to drop by a bit more and will also try to catch up with what I have missed.
Thanks
Neil
ive tried many plugins in the past and liked lot of them, i’m goint to try this one now an see how it works out. thanks
Thanks for the idea..Great post… amazing!
Hey Derek. Very interesting post but there are several plugins that show the recently popular posts without touching a single line of code.
While that is true, I like to try and keep the number of plugins to a manageable number so if I can leverage a plugin that I am already using and simply add a little code, then that is what I will do. Besides, I am thrilled with any excuse to write a little code.
Thanks for stopping by and sharing your comment Hasan!
A sign an excellent programmer – always considering code reuse, and enjoying writing code
It’s so easy to add a ton of plugins to wordpress – there are hundreds (probably thousands) that can do every conceivable thing to wordpress. I was wondering how many plugins you have on this blog ?
Joe@Expresso Content hopes you will read… Version 10001 released
There are roughly 20 plugins that are active on the site now, although some of those are just for back-end admin functions. That is still a considerable number of plugins and is more than I typically like to implement, but this blog is kind of my “playground” for experimenting with plugins and code.
I must ask though isn’t this just like the widget “Most Popular”? What is the main difference between this php and the Widget? Is it better results?
techlinkblog hopes you will read… 5 things to do when You get scammed and burned!!
You’re correct that it is quite similar to the “Most Popular” plugin / widget but the reason that I opted to go this route is because I was already using the Stats plugin and my belief is that any time you can perform a specific functionality without the overhead of adding an additional plugin, your site will benefit.
Thanks for stopping by and commenting!
You’re amazing! A little thinking and look what happens. You create a new approach to something old. It really seems like you know Wordpress backwards and forwards so I will be back with my many questions!
Dog Review Site hopes you will read… Chrome Choke Chains
I’ll be happy to help with your questions! I’ve got it down pretty well knowing WordPress backwards but I’m still working on the forwards.
i really appreciate your posts, i like the way you write. i am learning php.
Loved the idea, but actually learned the most from your June 7, 2010 comment about WHY this is better than a plugin. Thanks.
Kate The Phoenix Deal Lady hopes you will read… Monday at Zendejas Grill
hi derek,i have tried many plugins and now going to try your and i am too much excited to see how it works!!!!
Thanks.
best regards
sarah..
i also have tried lots of plugins,some were really very useful and some were not now i got one plugin from you,so now i am going to try it,thanks
james mathew…
Thanks! I came across this searching for the Most Popular Posts plugin! Steps are a little complicated but will give it a try!
This content so hot I’m freaking! Great way to display your top post, really attracted my view
Great tips, Derek. I liked “Being A Better Husband”. I could use the advice. Well, that article actually inspired me not just to become a better husband, but a better person. You’re a good man. Thanks!
{ 1 trackback }