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



@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