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












@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?
Eric,
I have been trying to get your plugin to work for the past few days with no luck. I thought another plugin might be interfering so I did a plain install with just my theme and no other plugins here: http://www.sixways.com/iphone/
On this blog I have both the cut and paste version going under my “popular” tab and the widget working. Unfortunately neither seems to pick up on any post views. Any ideas?
@John:
Can you verify if the recentlypopular table was created in your database during the installation?
Yes it created one table the database and it does have data in it. You can see the data in this screenshot: http://screencast.com/t/H6x7SXnCf
@John:
Well, that’s good. Probably means we’re having some sort of issue with the data being retrieved. Can you show me the widget settings and/or the php code you’re using to display the data?
@eric
Check email, I sent you log in information for this blog.
John
@John:
I looked and everything is fine on the UI. Could it maybe be a permissions issue in your database? I would check the web server and database error logs for clues.
I installed this plugin, i thought it would be easy to set up but after a week i still don’t see any posts in the lists. My site is http://www.e4g.info and under the Most Viewed it is suppose to display the top 5 posts but it hasn’t. Maybe i did something wrong? I am not using the widget i am using the php code.
@Dardin:
I’ll need to see the php code you’ve put into your template if I’m going to help. I would guess you and John are having the same problem and I’d like to get it sorted out.
<?php get_recently_popular(2, ‘DAY’, 5, 0, 2, ‘%post_title% Hits %hits%’); ?> that is the php code i put in my site.
Since the comment form decided to screw up the php code, i decided to upload a html to my site that has the code.
http://www.e4g.info/code.txt
@Dardin:
You’re missing the closing % on your %post_url%, though that shouldn’t stop it from outputting something.
Can you try putting in all of your categories for me and see if something shows up? It might be related to category filtering.