WordPress – Most Viewed – Sidebar Widget

| Created: August 20th, 2007
WordPress Hacks 17 Comments

I use Lester Chan’s WP-PostViews plugin to count how many views each post gets. I recently added a Most Viewed section to my Sidebar. The problem? I use WordPress Widgets and there is no widget for the plugin.

Around the time this post was written, Lester released a new version of the WP-PostViews plugin including support for widgets. There is no longer any need for the work-around explained in this post.

This post assumes you are using the WP-PostViews plugin and WordPress Widgets. If you are not, then this post is not for you.

Setting It Up

Fortunately, it is fairly simple to get this to work. First you need to get Otto’s ExecPHP plugin. This allows you have a ‘textbox’ widget which can execute php code.

Once you’ve installed and activated this plugin, go to the Presentation – Widgets (or Sidebar Widgets) page. You should now have the PHP Code 1 widget available (you can add more if you need them).

Drag the PHP Code 1 widget to wherever you’d like it to appear, then click on the configure icon on the right hand side of the widget. A dialog box will open. Enter an appropriate title, such as Most Viewed and put the following in the main section:

[php]
<ul>
<?php if (function_exists(‘get_most_viewed’)): ?>
<?php get_most_viewed(); ?>
<?php endif; ?>
</ul>
[/php]

Make sure you add the <ul> and </ul> before and after the php code. I originally left this out and found that the list was not formatted properly (because it wasn’t a list). I eventually found the solution from Otto himself on a WordPress Support page.

Close the dialog, click Save Changes and you should now have your Most Viewed section on your sidebar.

Changing The Default Behaviour

By default, the WP-PostViews plugin will display the top 10 posts and pages. It’s possible to change this by passing parameters in the get_most_viewed(); call. This is documented by Lester Chan on the plugin page, but here is a quick rundown:

The first parameter indicates what type you want: 'post' for posts, 'page' for pages, or 'both' (which is the default behaviour). The second parameter indicates how many results you want returned.

For example, I decided I only want 6 items (posts and pages) listed, so I replaced:

[php]<?php get_most_viewed(); ?>[/php]

with

[php]<?php get_most_viewed(‘both’, 6); ?>[/php]

Final Word

Having a Most Viewed section on your sidebar is a great way of keeping visitors on your site. If someone lands on one of your pages, they can quickly see your most popular posts, which are the ones most likely to entice them to browse your site further.

For someone with a content (rather than date) based site, such as myself, this is much more important than the monthly archive section.

If you use a different method to display popular posts, or have any other tips about keeping visitors on your site, please let me know.

17 responses on “WordPress – Most Viewed – Sidebar Widget

  1. WiMax Billing

    I was just looking at the widgets on WordPress. They provide a dismal selection. This one really intrigues me, and I think I’ll give it a try.

  2. Motorola Razr Skins

    This is the first time I have seen a website that keeps track of how many times a page has been viewed. This could be beneficial but I think would have to be implemented at the right time for newer sites.

  3. Stephen Cronin Post author

    Motorola, I like to know how many views a page gets, but when my blog was new, the numbers were embarassingly small. There’s still a lot of room for improvement!

  4. Stephen Cronin Post author

    Rudi, yes, there is now a widget built into the plugin. I use it too. You no longer need to use ExecPHP to create one.

    When I wrote this post, I was still using a version without the widget. I believe the widgetized version was released about the same time I wrote this post, making my post immediately redundant.

    Thanks for bringing this to my attention – I’ll add a note to the top of the post to explain this.

  5. Kristina Caban

    I use Popularity Contest also. The problem I have with Pop Contest is that I don’t want the percentages showed across all posts. Thanks for the plugin. I have been i n search of one of these function that will work best for me and I’m gonna give this one a try. Thanks again!

  6. Erick

    All these plugins are useless if one uses a cache such as 1BlogCacher or WP-Super-Cache. For views counting, it is therefore better to have some kind of an external Javascript that loads separately from the (static cached) page.

    1. Stephen Cronin Post author

      Hi Erik,

      I think the latest version of WP-PostViews counts the page views using AJAX, so it works fine with the cache plugins. If the latest one hasn’t been updated, there is definitely a hacked version out there somewhere which does this, and a commitment from Lester Chan to add this to the original..

  7. Steve the Admin

    I have been using Popularity Contest for a while now but the formula it uses is far more complicated than this. Most viewed is a simpler solution would give a better indication of what is getting looked at.

  8. Angeline

    Hi,

    I’ve installed it and its working well…

    BUT is there a way where I can choose the time range? I want it to detect ALL POSTS (from the FIRST day of my blog) instead of starting from the day I installed the widget….

  9. Angeline

    is there a way where I can choose the time range? I want the widget to detect ALL POSTS (from the FIRST day of my blog) instead of starting from the day I installed the widget….

    1. Stephen Cronin Post author

      Hi Angeline,

      Unfortunately, no… The plugin counts the page views – before you installed the plugin, these were not counted at all, so there’s no information for the widget to us… 🙁

  10. Eric@Aweber Alternative

    This is really awesome to use if you want to code some of your custom stuff into the sidebar. I use this to hide ads on my “money pages”.

  11. Ali Mostofian

    Hi,

    what do i need to do, if i only want to get most viewd in 2008 or in last 30 days?

    Thanks

  12. Cody@back pain, back pain relief, upper back pain

    Presently I am using Popularity Contest in my blog, but as someone mentioned in a prior post, it has a complicated formula.
    I would be interested in using this plug in, but I wonder when would be the right time to add it to a new site.

  13. Eduardo Nunes

    Thank you very much for the great info!
    This is all I wanted to have something like Webalizer working live at WordPress 😀

    Awesome!!!