How to track Post Views by Using WordPress Post Meta

15 May 2011 By Code Snippets 1 Comment
15
May

Post Views are a great way for us as Web Designer’s/Developers to gauge an understanding of how many hits or post views our WordPress articles are receiving. This code snippet demonstrates how to monitor the amount of views your WordPress posts are receiving.

Why Track Our Post Views?

You put some much time into writing the content for your website. The least you would like in return is to actually be able to see how many visitors are actually reading your content. The image below displays how the article or post views are displayed on the WPInsite website.

Tracking and Display our Post Views

Tracking and Display our Post Views

Tracking Our Post Views

Tracking our post views is a fairly simple task. I will give your two options in the article. The first option is by adding code to your WordPress theme “functions.php” file which is found in the directory of your current theme.

Option 1: Adding code to our functions.php file

Step 1 is to paste the below code into your WordPress functions.php located in your theme directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
   }
   return $count.' Views';
}
 
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
   }
   return $count.' Views';
}

function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

Next, we need to call the function in out WordPress theme. Place this below code inside the single.php file inside the WordPress loop.

1
2
3
<?php
    setPostViews(get_the_ID());
?>
<?php
    setPostViews(get_the_ID());
?>

Finally, place the below code in any of your templates where you would like to post views to be displayed.

1
2
3
<?php
    echo getPostViews(get_the_ID());
?>
<?php
    echo getPostViews(get_the_ID());
?>

Option 2: Using the WP PostViews WordPress plugin

Using the WP PostViews plugins is a far tidier way if you are not comfortable or do not wish to manually go in and modify your theme files. The plugin has a single task: Enables you to display how many times a post/page had been viewed.

This plugin is great in the way that it allows you to display your statistics in widgets. You can display:

  • Standard post views
  • Display least viewed posts
  • Display most viewed posts
  • Display least viewed posts by tag
  • Display most viewed posts by tag
  • Display least viewed posts for a category
  • Display most viewed posts for a category
  • and more…
Download the WP PostViews Plugin

Other Post View and Post Tracking Plugins Available

There are quite a number of WordPress plugins available to track our post views. Below I will list five plugins that are available to download from the WordPress Plugin suppository.

Page ‘n Post Views

Reports the number of views for pages and posts, in the admin lists and can be used in the front end.
This very simple plugin keeps a count of your post/page views:

  • In the admin post and page tabs, it adds a neat column that tracks the number of views.
  • In the front end, you can display page or post view by adding the following code in your templates:. The default output will be something like: 235 views

This plugin requires the new JetPack by WordPress.com or the old WordPress.com Stats plugins.

Download the Page ‘n Post Views Plugin

Post-Views

A plugin logging how many times your post/page has been visited, supporting cache plugin, with detailed analysis pages and powerful external invoking functions. (import views data from wp-postviews if used.)

Download the Post-Views Plugin

WP-post-view

Easily display the views visited of each post. Tracks the views in each posts visited, views number are also display in each row of the post in the ad.

For Users

This plugin allow you to display every visits/views count in each post. This plugin counts every views as long as you refresh the page or view the page, it does not uniquely capture visitors. Easily copy paste of 1 line of code into your WordPress file to display the views in the post.

For Developers

This plugin also contain detailed source code for developers who just started plugin development for WordPress, a suitable plugin source code to take a look on how to use database manipulation, commenting and simple hooks. I hope this would assist you in your learning journey for WordPress plugin.
[box_download]Click to download [/box_download]

Download the WP-post-view Plugin

Simplicy post view

Simplicy post view is a widget that displays one or more items to your sidebar with several options:

  • Select an item or category
  • The item number is displayed (if a category is selected)
  • Display of the extract or not
  • Character limit in the extract (if the option Show the extract is checked)
  • Display of miniature or not
  • Dimensions of the miniature in order to fit your theme (if the option Show thumbnails is checked)
Download the Simplicy post view Plugin

Post Views Summary

Post Views Summary is a dashboard plugin that shows the most and least visited posts and pages.

Download the Post Views Summary Plugin

As you can see there are many options and plugins available for us to track our post views.

Be sure to check out our other great WordPress Code Snippets and WordPress Articles.
Tags :
Enjoy this article? If so, we would love to hear your thoughts in the comments below

 

Related Blog Posts

One Response to How to track Post Views by Using WordPress Post Meta

  1. How do I create a function in functions.php to display the number of the post before the contents of each.

    so

    Title
    1 – content of the post 1

    Title
    2 – content of the post 2

    Title
    3 – content of the post 3

    Sorry I do not speak English

    I use google translator

    Reply

Leave a Reply