There are all sorts of web statistics plugins and services that will tell you how many views a certain post received. And they certainly have their place. However, sometimes you may just want that information conveniently placed on the front of the post itself.

And so in this post we’re going to go over how you can get that, and also how you can hide that view count from certain viewers while showing it to others if you like.

binoculars-800px

WP PostViews Plugin

The first thing you will want to do is to pick up the WP PostViews plugin.

While there are a number of post view plugins out there, this one has an option to discount the views of bots to a page – something pretty important, obviously, but also something a number of these plugins don’t do.

In order to show the view count on a post, you merely need to put the following code into your single.php template:

1
<?php if(function_exists('the_views')) { the_views(); } ?>

That’s easy enough, but now we’ll go over controlling who can see that view count.

Choosing Who Sees the View Count

Some built-in controls for determining who sees the view count.
Some built-in controls for determining who sees the view count.

Another nice aspect of this plugin is that it actually comes with some built-in control to determine who sees the view count (and on what type of page too – post, homepage, etc.).

You can choose to show the view count to everyone, to only logged in users, or to only logged out users.

This is a nice function, but it’s a little limited. And so we’re going to hack that a little and make it even more flexible. (Thanks to fellow WPMU DEV writer Chris Knowles for this.)

One a site with a number of different writers, it seems only logical that you might want to let the writers see the number of views on posts they have written. For whatever reason, you might not want others to see that, people such as the general public, other writers, or visitors who have some kind of low level permission like “Subscriber” on the site.

And so here’s the code that will let you do that:

1
2
3
4
5
&lt;?
if ( current_user_can( 'edit_post' , get_the_ID() ) &amp;&amp; function_exists( "the_views" ) ) {
the_views();
}
?&gt;

By checking the “edit_post” capability and passing the post_id, we restrict the view count to the author of the post, the Editor and the Admin.

Whether limiting the viewers or not, here’s what the basic output will look like.

Wherever you place the code in your template files is where the view count number will appear. If you’d like to make it display with parenthesis, for example, just place parenthesis around the code — e.g. (the code).
Wherever you place the code in your template files is where the view count number will appear. If you’d like to make it display with parenthesis, for example, just place parenthesis around the code — e.g. (code here).


Other Functions in the Plugin

While the main purpose of this post is getting the view count of an individual post onto the post page itself, there are a number of other functions the plugin offers (another reason for choosing this one).

Widget settings.
Widget settings.

In addition to coming with a widget, you can insert code into your template files to create a number of automatically generated lists, such as …

  • most viewed posts
  • least viewed posts
  • most views posts by tag
  • least views posts by tag
  • most views posts by category
  • least views posts by category

See the plugin’s homepage for more info on all this.