Justin Tadlock

Post view count extension for plugin and themes

When developing the News theme, we needed a way to get the number of times a post (or any post type) was viewed.

What we’ve developed is a lightweight file that updates post metadata with the meta key of Views. This will keep track of each singular page view a post gets. We’re giving this script away for you to use in your projects. Think of it as a starting point that you can build all sorts of cool functionality on top of.

Complications

If you’re familiar at all with updating post meta, this would sound like an easy task. However, some browsers like Firefox prefetch URLs. What this means is that any link with rel="prefetch" or rel="next" will be pre-loaded as the browser is trying to guess what page you might go to next. Unfortunately, this has the side effect of artificially inflating the view count.

This script fixes that problem.

I’m telling you this up front because some of you may be wondering why this extension uses Ajax. JavaScript isn’t prefetched, which makes it a good solution. Of course, any user with JavaScript turned off in their browser will not register as a view.

Adding the Entry Views extension to your theme

You can use this in both plugins and themes, but this tutorial will show you how to add it to a theme. I assume most plugin authors can figure out how to add it to their plugin.

The first step is to download Entry Views. Unzip the file and add the entry-views.php file to your theme folder.

Then, add this line to your theme’s functions.php file:

if ( !function_exists( 'entry_views_update' ) ) require_once( TEMPLATEPATH . '/entry-views.php' );

Making Entry Views work

The Entry Views extesion will handle all the complex stuff for you. However, it works based on post types. For example, you could use this for the “post” post type but have it disabled for the “page” post type. It’s completely customizable.

So, let’s suppose we want it to update the view counts for the post, movie, and recipe post types. We’d add this to our functions.php file:

add_action( 'init', 'my_add_post_type_support' );

function my_add_post_type_support() {
    add_post_type_support( 'post', array( 'entry-views' ) );
    add_post_type_support( 'movie', array( 'entry-views' ) );
    add_post_type_support( 'recipe', array( 'entry-views' ) );
}

You can also register support in the supports array when adding a custom post type like so:

register_post_type(
    'example',
    array(
        'supports' => array( 'entry-views', 'title', 'editor' )
    )
);

Doing one of those two things will make posts of your post type(s) available to the Entry Views script. It will count the number of times a singular post has been viewed. Note that this does not count views on archives, search results, and the posts page.

Showing the view count

There are two ways to show the view count. You can use a template tag or shortcode.

To use the template tag, drop this somewhere within The Loop:

<?php if ( function_exists( 'entry_views_get' ) ) entry_views_get( array( 'before' => 'Views: ' ) ); ?>

To use the shortcode, add this in a shortcode ready area within The Loop:

[entry-views before="Views "]

Both the template tag and shortcode will take in one of three arguments:

  • before: Text to show before the view count.
  • after: Text to show after the view count.
  • post_id: Show the view count of a specific post. This defaults to the current post ID.

Overwriting the default meta key

Some people may have used other view count plugins or functions in their themes before, so Entry Views has a filter hook to change this. Its default meta key is Views. If you need to customize this for whatever reason, just add a custom filter to it like below.

add_filter( 'entry_views_meta_key', 'my_entry_views_meta_key' );

function my_entry_views_meta_key( $meta_key ) {
    return 'custom_view_key';
}

Building your own features on top of Entry Views

The code we’re handing over is fairly basic. It didn’t make much sense to build a lot of custom functionality directly within it because there are so many different things that can be done. It made more sense to leave it flexible and make it reusable for others.

One good example is from our News theme. It has a built-in Popular Tabs widget that allows you to select a specific post type and display the view count in one of the tabs.

It uses an instance of WP_Query to query posts by the number of views. Here’s what a basic query and loop would look like to show the most-viewed posts:

<?php $loop = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => 10, 'meta_key' => 'Views', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) ); ?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

    Add custom HTML and functionality here.

<?php endwhile; ?>

I don’t want to limit your imagination though. You can do all sorts of things. Mix and match post types. Pull the least-viewed posts. Get the most-viewed posts from a category or even for a specific month. Just have fun with it.

Get the code

If you make use of this code, please let us know how you’re using it in your projects.