“Recently Popular” WordPress Plugin
*** UPDATE ***
Please Read This Post If You've Been Experiencing Problems
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 four tags: %post_url% - the post's permalink %post_title% - the post's title %hits% - the number of qualifying views %display_name% - the post's author 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 get_recently_popular($interval_length, $interval_type, $limit, $user_type, $post_type, $output_format, $categories); ?> </ul>
Where:
- $interval_length is the number of $interval_type to consider.
- $interval_type is the type of interval. This needs to be the string HOUR, DAY, WEEK, or MONTH
- $limit is the maximum number of posts to display.
- $user_type is the types of users you want to count views from. 0 = All, 1 = Anonymous only, 2 = Registered only.
- $post_type is the types of posts you want to count views from. 0 = All, 1 = Pages only, 2 = Posts only.
- $output_format is a formatting string for how to display each result in the list.
- $categories is a string formatted as: " 'Category 1', 'Category 2' ", including the single quotes, with every category you wish to include listed. If this value is not supplied or is an empty string then all categories are considered.
For example, this will show the ten most viewed pages and posts in the past one month by users who weren't logged in:
<ul> <?php get_recently_popular(1, 'MONTH', 10, 1, 0); ?> </ul>
While this will show the five most viewed posts in the past two weeks by all users:
<ul> <?php get_recently_popular(2, 'WEEK', 5, 0, 2); ?> </ul>



[...] – 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?
@brando18:
Make sure you have both the Recently Popular plugin *and* the Recently Popular Widget plugin activated.
[...] – 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 [...]
Great plugin, thank you. Is there supposed to be a control panel with this one, like the “image dumps” on the download page of WP, as mine does not seem to have this. Working fine though as a widget on my pages, thank you
@Business blog:
No, right now there isn’t a configuration page. All of the configuration options are in the widget so that each widget instance can be completely customized.
[...] – 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 [...]
I just added your great plugin to my site, but I Just wanted to confirm a couple of things…
1) I’ve got WP-Cache installed. Will your plug-in keep proper track of views and update even with this installed? Are there any special caveats?
2) I get a significant amount of traffic on my site, and wanted to make sure that using your post won’t eventually slow things down to keep track of visitation stats. Can you briefly explain how you’re tracking popularity?
@technabob:
1) Even with WP-Cache installed the hits will be recorded correctly. The caveat to that is that the outputted list will only be as fresh as the last time the page was cached, so if you are displaying view counts they won’t be any more accurate than your cache expiration allows them to be. I use WP-Cache on this site with no issues.
2) Popularity is tracked by writing an entry to a table on every page view that is either a post or a page. If the view is a listing, search page, etc, then nothing is done. The plugin simply stores a timestamp, the type of user (logged in or not), and the id of the page that was viewed. In MySQL 5 this comes out to 27 bytes per row, with 20 bytes consumed by the post id in order to conform to WordPress’ column size (BIGINT(20)). In some future version I plan to add a settings page with an option to purge old data since this isn’t intended to be a stats package.
Popularity is determined by a single select statement with some grouping. The columns are indexed, so the query is very fast. It only groups the records which meet your time criteria.
If that doesn’t answer your questions let me know.
I would also like to exclude some of the pages/posts from the chart?
Is it possible to display the most viewed pages from a specific level of the page hierarchy?
Pages have no category so I cannot do this as if they were posts.
Or is it possible to just exclude those few pages that I do not want in the most viewed chart?
Or is it possible to not count certain pages?
@Stems:
Right now I don’t have anything like those features planned. Your needs are pretty specific. You would probably be best served modifying the plugin yourself and using your own custom version.
Weird, I installed it and activated it, but I can’t find it in my wp admin, and its not available as a widget. Tips?
@Morten Skogly:
Did you activate both the plugin and the widget? There is no admin page yet. The widget is the only user interface.
On Matt’s issue – any progress? I’d imagine that those using the function call directly (as I am) would much prefer the ID, with children included, approach too. Ideally (at least in my case) the ability to exclude rather than include cats would be useful also.
But it’s a brilliant plugin – thanks.
Hi,
I just installed your plugin as it sounded like what I wanted.
I’m running wordpress 2.7, I looked at the table in my database and it was added recent_popular, but there is no data in it. I click on a bunch of pages and I’m sure my site is getting hit constantly but it never seems to update…
Does this work with wordpress 2.7? I posted a question last night, it seems to be gone….
I enabled and installed the plugin but nothing is going into the database.
I have 2.7.1 … after activation, your plugin does not appear in “settings” or in “wigets” section of appearance. I see you are running 2.7.1 on your own site, so presumably it is working for you. How do you get it to work on another site (also running 2.7.1)?
@Katherine:
What little asking I’ve done has actually led me to believe people want the category name, not the ID. I may take up the hybrid approach and just split the query. Work is pretty hectic right now, so it will be a couple of weeks before I can get back to this.
The include/exclude argument is as old as the hills. In this type of scenario I typically fall into the “less is more” camp so that things aren’t accidentally exposed.
The source code is of course available from the plugin installer page and I’ll happily review and accept patches. :)
@James:
Sorry, I moderate all comments (you wouldn’t believe some of them). Sometimes it takes me a while to get them approved.
As for your problem, typically that is the result of not activating the plugin. If that isn’t the case I’d love to hear about it and I’ll work with you to sort out the problem.
@EL:
At risk of sounding repetitive, it seems you have the same problem. Make sure you’ve activated both the “Recently Popular” plugin and the “Recently Popular Widget” in the Manage Plugins page. All configuration of this plugin is done through either the widget or direct function calls if you aren’t using the widget.
Hi Eric,
Thanks for the reply! The plugin is active, it shows in the list of active plugins in the admin menu. I am not running the widget though, but from what I gather I shouldn’t need to, it looks like it just uses a hook into wordpress.
Perhaps something did not install properly. Is there something I can look at to help debug this?
Also to note, I attempted to deactivate and re-activate, it believes it is active but no database entries occur.
@James:
You are correct that you don’t need the widget to be activated. The plugin will happily collect stats without ever displaying them and you can display them without the widget by directly calling the function.
Can you tell me what other plugins you are using? Maybe there is one the is interfering with the events that Recently Popular hooks into.
These are the plugins I am using:
Akismet
Feedburner
Google XML Sitemaps
Simply Exclude
Stewart’s Single Page Viewer
Top Level Categories
wp-cache
and of course Recently Popular
@James:
I’ll try and do some testing next weekend but this week I’m completely buried at work.
Up front I can tell you that Akismet, Google XML Sitemaps, and wp-cache are all fine. It is possible that the Top Level Categories plugin is the culprit, and I wasn’t able to find Stewart’s Single Page Viewer.
Single Page viewer probably wouldn’t be as it only affects the final display query in the template.
I could see top level categories being the culprit as it does affect how wordpress does lookups I believe.
Thanks
Hi Eric, I see your plugin has gained quite a popularity :) Glad to see that… I see you’re quite busy with other things but just wanted to check is there any news about options we discussed earlier?
Good luck and thanks again…let me know if there is anything I can do for you
@perke:
Thanks. It does seem to have filled a need.
I’ve actually done most of the work for your request and committed it to the repository up on Wordpress.org. I think the last snag was getting the actual image paths to work.
With any luck my time will free up some after this week.
[...] Recently Popular [...]
Hi Eric,
thanks for that great plugin.
Just wanted to know, does it take some time for the popular posts to appear?
I’m using the non-widget plugin (both are activated in my settings) and it is hardcoded in my index.php (is it okay to use in index.php?) like this : <?php get_recently_popular(1, ‘WEEK’, 5, 0, 0, ‘%post_title%‘); ?>
Thanks much for your help.
PS: you’ll find my site url in the link above – plugin is hidden now you can search for ‘popupost’ in the code though.
@Tomakun:
It depends on which index.php you put the code into. It needs to be the index.php file in your templates directory, or one of your other template files depending on your needs.
Have you checked the database to see if the Recently Popular table is being populated with rows?
Ok Eric, I was a bit impatient and went over to WP svn and got new files :) and I can happily confirm that everything looks great!
I’m using it in template like this
<?php get_recently_popular(2, ‘WEEK’, 7, 0, 2, ‘%post_title%%publish_date%’); ?>
Many thanks and good luck!
oh well, guess this piece of code got stripped cuz of html… nevermind, all is good :)
[...] the plugin called Recently Popular and activate it. No need to activate the Recently Popular Widget because we are using the function [...]
Hi
I started using this plugin on my site above with getting the top 10 posts over 30 days with view counts. Using WP Tuner plugin to check the performance of the sql executed, it seems too much for the php to handle, which prompted my ISP to disable my account often.
Changing to top 10 posts over 14 days significantly reduced the execution to under 500ms.
Is this expected?
@Teng-Yan Loke:
The fewer records you ask the database to summarize the less time the database execution will take. I haven’t heard any other complaints about the execution speed, though. How many hits a day to you typically get?
Hello
Is there a ’safe’ way of resetting the data, as if the plugin was installed fresh? Or do I need to empty the db table?
thanks
@sanjay:
For now you will just need to empty the Recently Popular table manually. One of the future items is a settings page that will let you purge records greater than a certain date.
Excellent, just gotta recomend this….
got my thumbs up….
Dear Eric,
thanks again for the plugin.
I’m searching for a way to pull out a custom field from my posts, to the posts that are displayed in the Popular Posts list.
Basically I just want to call out a custom field that as ‘Image’ as value. Any thoughts? Looking at your plugin file it looks like it would be quite simple to realize, however I’m not really good at php…
Thanks for your help.
What would be nice is also to be able to pull out excerpts & comment count… and of course a custom field. Maybe for the next version?