Content So Hot You'll Freak

by derek on May 11, 2010 · 28 comments

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


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


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:


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!

Stay In The Loop!

Subscribe to the Derek Semmler dot com feed via RSS or Email to receive notifications when new posts are published. Follow the WordPress ninja on Twitter too!

{ 27 comments… read them below or add one }

Kelvin Kao May 11, 2010 at 6:08 pm

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 =-.


derek May 11, 2010 at 10:34 pm

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.


Adam Haver - Utah Graphic Designer September 9, 2010 at 11:16 am

I was wondering the same thing about PHP – I guess you just have to find people you can trust.


ipad skins May 17, 2010 at 4:32 am

Great post… Interesting one. Thanks for the idea.
.-= ipad skins hopes you will read… Apple ipad Skins =-.


Diyetteyim May 25, 2010 at 5:16 pm

Where can I change post limits ?


derek May 25, 2010 at 9:41 pm

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.


Paul Evans May 26, 2010 at 6:42 am

had a good time reading this one.. πŸ™‚


My Business Is Fitness June 1, 2010 at 5:41 am

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!


derek June 1, 2010 at 10:21 pm

Ben, thanks for the comment. Please feel free to ask any WordPress questions and I will do my best to help you out.


Free Stuff June 2, 2010 at 3:08 am

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.


rob June 3, 2010 at 4:19 am

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


Doreen June 3, 2010 at 9:34 pm

Thanks for the idea..Great postÒ€¦ amazing!


Hasan Akhtar June 4, 2010 at 8:13 am

Hey Derek. Very interesting post but there are several plugins that show the recently popular posts without touching a single line of code.


derek June 4, 2010 at 1:41 pm

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!


Joe@Expresso Content January 6, 2011 at 3:05 pm

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 ?


derek January 8, 2011 at 10:03 pm

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.


techlinkblog June 7, 2010 at 4:31 pm

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?


derek June 7, 2010 at 6:19 pm

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!


Dog Review Site June 13, 2010 at 9:19 am

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!


derek June 13, 2010 at 10:54 am

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. πŸ˜‰


philip - Online Auction June 25, 2010 at 4:59 am

i really appreciate your posts, i like the way you write. i am learning php.


Kate The Phoenix Deal Lady July 25, 2010 at 4:35 pm

Loved the idea, but actually learned the most from your June 7, 2010 comment about WHY this is better than a plugin. Thanks.


sarah July 28, 2010 at 12:00 am

hi derek,i have tried many plugins and now going to try your and i am too much excited to see how it works!!!!
best regards


james August 4, 2010 at 10:56 pm

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…


Alek | Deposit Bonuses October 27, 2010 at 12:07 pm

Thanks! I came across this searching for the Most Popular Posts plugin! Steps are a little complicated but will give it a try!


South Africa Car Rental November 16, 2010 at 3:09 am

This content so hot I’m freaking! Great way to display your top post, really attracted my view


Brian Kinkade@franktown real estate August 4, 2011 at 10:54 pm

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!


Leave a Comment

{ 1 trackback }

Previous post:

Next post: