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

Recently Popular Widget 0.6 (simple)

Recently Popular Widget 0.6 (simple)

Recently Popular Widget 0.6 (formatting options)

Recently Popular Widget 0.6 (formatting options)

Recently Popular Widget 0.6 (category options)

Recently Popular Widget 0.6 (category options)


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>