Track and Display Post Views Count Without a plugin using post meta

In this post i will tell you How to track and Display Post Views Count without a plugin using post meta in wordpress.Use the following code to track post views on your wordpress blog.

The first thing you have to do is to add the following code to your WordPress Theme functions.php file or Your wordpress Plugin file.

function mct_getPostViews($postID){
    $count_key = 'mct_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 mct_setPostViews($postID) {
    $count_key = 'mct_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);
    }
}

// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); 

Step 1:Call the function in your single post loop.

Now we have the function mct_setPostViews which we need to call this function on single post page i.e single.php of your wordpress theme.
To do this you need to copy and paste the following code in your wordpress single post loop:

mct_setPostViews(get_the_ID());

But if you wants to make things simpler then you need add this code on your Theme functions.php file.

function mct_track_post_views ($post_id) {
    if ( !is_single() ) return;
    if ( empty($post_id) ) {
        global $post;
        $post_id = $post->ID;    
    }
    mct_setPostViews($post_id);
}
add_action( 'wp_head', 'mct_track_post_views');

One you add the following code,every time a user visit the post, the custom field will be updated.

Step 2:Call the function to show the post views.

Now what you need to do is that add the following code where you wants to show the post views in your post.

[moptin id="3098"]
mct_getPostViews(get_the_ID());

Final code for Post views Count

Your Theme functions.php file or Your Plugin file will look like:

<?php
function mct_getPostViews($postID){
    $count_key = 'mct_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 mct_setPostViews($postID) {
    $count_key = 'mct_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);
    }
}

// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); 

function mct_track_post_views ($post_id) {
    if ( !is_single() ) return;
    if ( empty($post_id) ) {
        global $post;
        $post_id = $post->ID;    
    }
    mct_setPostViews($post_id);
}
add_action( 'wp_head', 'mct_track_post_views');

//Other functions code goes here

?>

Do you Like this Post? If Yes then please share it with your friends.

Shubham Kumar

Hey, I am Shubham and i love Blogging, Coding and exploring new things and obviously sharing my experience with you.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *