"Recently Popular" WordPress Plugin
Download the Recently Popular plugin
I’ve used a few different plugins to manage the most popular post display on my sidebar. None of them behaved quite the way I wanted. The two major groups and their issues for me were:
- Post counts last forever: This is a big deal if an article gets Dugg or Slashdotted. That post may live at the top of your most popular list long past its useful lifespan.
- Formula-based: These use some formula of views, comments, and who-knows-what-else to try and derive what is most popular. These also count things into perpetuity, but generally they discount dated items as they age. The fact that views aren’t dated is still a problem.
I knew what I wanted. I wanted a popularity meter that I could tell “Show the posts that have been most viewed over the last 7 days.” Or 3 weeks. Or 12 hours. Or whatever. However I couldn’t find it. So I wrote a plugin (with widget!) to do just that. It records hits to any post or page on your site by any user who isn’t logged in, then allows you to display the most active ones for your defined time period on your sidebar.
The widget can be added to your sidebar more than once, so if you want to show the most popular over the last five days and the most popular over the last month you can.
Output formatting
The widget supports formatting strings for items in the results. These formatting strings accept HTML, so you can be as creative as you want. The template supports the following tags:
- %post_title% – the post’s title
- %post_excerpt% – the post’s excerpt
- %post_url% – the post’s permalink
- %hits% – the number of qualifying views
- %display_name% – the post’s author
- %user_url% – the post author’s URL
- %publish_date% – the post’s publish date
- %category% – the categories the post is in
- %thumbnail_url% – the URL for the post’s thumbnail
To simply show the post titles as links to the post you would use this tag (note: this is the default output format):
<a href="%post_url">%post_title%</a> |
The widget defines this and two other typical tags that you to simply click on and use without having to figure out the templating.
Using in Themes Without Widget Support
To install this plugin in a theme that doesn’t support widgets you add something similar to this to your template:
<ul><?php $rp = new RecentlyPopular(); $rp->get_counts(); ?></ul> |
The get_counts() function takes a single parameter that is an array of the options. The options and their defaults are (default options in bold where applicable):
| Option | Values |
|---|---|
| Description | |
| title | ‘Recently Popular’ |
| Title for the emitted ul. Primarily used by the widget. | |
| enable_categories | true false |
| Determines if category filtering is used. | |
| categories | n/a |
| Category names in single quotes, separated by commas. Ignored if enable_categories is false. | |
| date_format | ‘Y-m-d’ |
| PHP date format to use for date output tags. | |
| default_thumbnail_url | n/a |
| URL to a default thumbnail image to use when no thumbnail is found. Prevents empty images from showing. | |
| display | true false |
| If true the call will emit an unordered list, if false it will return that list from the function call. | |
| interval_type | ‘HOUR’ ‘DAY’ ‘WEEK’ ‘MONTH’ ‘YEAR’ |
| The type of interval to use. | |
| interval_length | 1 |
| Integer value for the number of interval_type to display. | |
| limit | 10 |
| Integer value for the number of results to display. | |
| max_length | 0 |
| Integer value for truncating the title. 0 means no limit. | |
| max_excerpt_length | 0 |
| Integer value for truncating the excerpt. 0 means no limit. | |
| output_format | ‘%post_title%‘ |
| String defining how each result will display. See Output Formatting above. | |
| post_type | RecentlyPopularPostType::ALL RecentlyPopularPostType::PAGES RecentlyPopularPostType::POSTS |
| The type of results to get. | |
| user_type | RecentlyPopularUserType::ALL RecentlyPopularUserType::ANONYMOUS RecentlyPopularUserType::REGISTERED |
| The type of results to get. |
For example, this will show the five most viewed posts (without pages) in the past three weeks by users who weren’t logged in:
<ul><?php $rp = new RecentlyPopular(); $rp->get_counts(array( 'limit' => 5, 'post_type' => RecentlyPopularPostType::POSTS, 'user_type' => RecentlyPopularUserType::ANONYMOUS, 'interval_length' => 3, 'interval_type' => 'WEEK', )); ?></ul> |



[...] Recently Popular [...]
Thanks for your fantastic plugin, I really love it.
Here’s one suggestion I have though. Do you think we could have an option to exclude some of the pages/posts from the chart? I have some dynamic pages which make use of GET parameters to show different contents (one of them a forum). That means users load these pages over and over again, and they can easily top the other regular yet more substantial posts/pages.. Now I have do to the database and remove these page hits from time to time to avoid them showing up on the chart, but if I can simply exclude them it’ll be really nice.
@Thomas Tsoi:
I’m glad you like it. I can certainly add the ability to ignore certain post/page IDs in the future. It sounds like maybe it would be more useful if you could not record any hits to specific ULRs using wildcard matching and/or be able to support hits to non-WordPress pages.
[...] January 3, 20092:46 am WordPress Plugins 01/03/2009 Recently Popular records hits to any post or page on your site by any user who isn’t logged in, then allows [...]
hi. Thanks for plugin
perfect.
Regards
Ok everyone: 0.4.7 is out a day early. Happy new year.
I was happy to see the plugin update notification in my WP-Admin this morning – thanks for following through, Eric!
I’m able to do exactly what I need for my client site now. But I do have a suggestion and one more request.
If instead of category name, the ID could be used – that would prevent any unnecessary template updates if the category name were to change.
I’m not sure what’s involved, but would it be possible to include child categories automatically? Again, trying to ensure my client can make changes to their category structure without having to update templates all the time. The site I’m working on has 3 main categories, and child categories will be added/removed regularly. Which means, I’ll have to jump in there and update the templates each and every time. Where as, if children were supported, I’d just have to specify the main categories and leave it be.
@Matt:
I can certainly look into that. I don’t think the child category bit would be too hard, but right now I’m talking without looking. I’ll let you know when I’ve had some time to look at it.
Hi!
First; seems to be a great plugin, but…
So, what´s wrong?
I´ll use the plugin in my template – so I put the code …. in my template. But it does not work. Only 1 post is shown, an these one is not really popular….
Please help!
@Thomas:
Can you show me exactly what you put in your template? That would certainly help diagnose any issues.
Sorry Eric, my mistake!
Everything´s fine! So your plugin counts the “readers” since instalation. And I thought, that all “readers” are count since the article was wrote.
And sorry for my bad school-english. :/
So far. Greets from germany
Thomas
@Thomas:
No problem. WordPress doesn’t track the data necessary for this plugin, so it adds a table and populates it with each page view. So when it’s first installed it won’t have anything to show.
If anyone is concerned about having an empty list display then you can just enable the plugin and not put it on a page anywhere. It will collect data whether it is displaying data or not, then when you feel like you’ve collected enough data start displaying it.
Hi Eric, great work with the changes! Thanks much and donation is on the way.
Can I feel free to ask another question in regards of further hacking your plugin via email?
@perke:
Fire away. I sometimes don’t get my email more than twice a day, but it will get a response.
Thank you for creating a plugin that does what I want and actually works. You have earned yourself a ‘mini post’ on my blog.
[...] Biven’s ‘Recently Popular’ WordPress plugin just works. Rate this: 2.8 Share and [...]
@Matt:
I haven’t forgotten about you. I’m trying to weigh your request for using ids vs the people who are using the function call directly and not using the widget. Using category ids for them would be difficult at best. I may take some sort of hybrid approach, but I need to weigh that against the increased complexity of the joins.
[...] – produce a list of popular posts for specified categories. That was until I found the brand new, Recently Popular plugin. At the time I stumbled upon it, the functionality I was after wasn’t quite there. But after [...]
[...] – produce a list of popular posts for specified categories. That was until I found the brand new, Recently Popular plugin. At the time I stumbled upon it, the functionality I was after wasn’t quite there. But after [...]
[...] – produce a list of popular posts for specified categories. That was until I found the brand new, Recently Popular plugin. At the time I stumbled upon it, the functionality I was after wasn’t quite there. But after [...]
[...] – furnish a list of renouned posts for specified categories. That was until I found the code new, Recently Popular plugin. At the time I stumbled on it, the functionality I was after wasn’t utterly there. But after [...]
[...] I found the brand new, Recently Popular plugin. At the time I stumbled upon it, [...]
[...] – produce a list of popular posts for specified categories. That was until I found the brand new, Recently Popular plugin. At the time I stumbled upon it, the functionality I was after wasn’t quite there. But after [...]
Hello! This plugin seems fantastic, but I’ve got a bit of a problem. I’m trying to write this feature into my custom theme (non-widgetized) and I’m having some difficultly formatting the end of the code…
Your example don’t show the syntax for $output_format or $categories. Can you possibly steer me in the right direction?
Thanks!
Tyler
@Tyler:
$output_format is a string using the substitution keys shown above.
%post_url% – the post’s permalink
%post_title% – the post’s title
%hits% – the number of qualifying views
%display_name% – the post’s author
You can mix and match these into any HTML you want. The plugin will automatically wrap them in an <li> element. The example above shows a simple link to the page, using the post’s title, like so:
“<a href=”%post_url”>%post_title%</a>”
$categories is passed as a single string with each category contained in single quotes and separated by commas.
So, if you wanted to show the top 5 posts or pages for the last three months viewed by any type of user, displayed with the post title as a link and the author’s name following it, only from Category A and Category B, it would look like this:
<ul> <?php get_recently_popular(3, ‘MONTH’, 5, 0, 0, ‘<a href=”%post_url”>%post_title%</a> by %display_name%’, “‘Category A’, ‘Category B’”); ?> </ul>
If that doesn’t help then give me a little more guidance about exactly what you’re trying to get and I can be more specific.
(I left the last two parameters out of the last two examples because they both have sane defaults and I wanted to keep them simple)
Wow I’m sorry. That part just flew over my head yesterday. I totally understand now. Thank you so much for the timely, patient response. The plugin works great! (especially now that I’ve gotten past my brain fart) I do really enjoy the ability to set a time period.
I did have another question, though. Is there any way to grab more meta data from the posts to display in the list (ie. category or perhaps even custom fields)? That would truly be fantastic.
Thank you so much for this excellent plugin!
Tyler
@Tyler:
No problem, and you’re welcome.
As for getting more information: I’m currently working on exposing some more information for perke. I haven’t had a lot of time to look into it, but I’m considering trying to change the way I am exposing items to the formatting string to comply more with the WordPress standard post object so that more things would become available.
[...] exactly the functionality I desired. At the time being. That was until I found the brand new, Recently Popular plugin. Unfortunately, most are now geared towards WordPress 2.7, and DPS is running 2.6.5 for the sidebar [...]
Hello, new to WP. I’ve used 2.7 to install the zip file of this plugin. The admin panel said this is installed. Yet I cannot see the widget in my widget manager. what am I missing?