How to Count Post Views Without using WordPress Plugin

0

How to Count Post Views Without using WordPress Plugin

Many of us having Wordress websites or blogs. So we like to cout post views of our post or page. There is lot of plugins there in wordpress. But we already got irritated having lot of plugins for many purpose.  so if you don’t want to add more and more plugins to our website.  This is easy step for adding simple code for Count Post Views.

Step 1 : Use the below to track the count post views on your wordpress blog or page content. You have to add this code in functions.php of your wordpress theme. This code is used to track your view counts

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);
    }
}

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

 

Step 2 : Add this code to single.php page inside to track the count of each post or pages. This part will track the count of view

Cinema Latest news - Cinema News | Tamil News | Tamil Cinema | Daily Cinema | Tamil Cinema Trailer | Tamil Cinema Teasers | Tamil Cinema reviews | Tamil Cinema News | Tamil Cinema Reviews | Tamil Movie Reviews | Kollywood News - provided Latest cinema news, Tamil cinema updates, cinema exclusive news, video, audio, photos, movies, teasers, trailers, entertainment and other Tamil cinema news 24/7 updates

<?php
          setPostViews(get_the_ID());
?>

Step 3 : This code is just echo part to print the view counts. So you just placed the code where you want to display tha count in you page. you may placed in in single.php or index.php. you better placed in single.php. Then see the output

<?php 
          echo getPostViews(get_the_ID());
?>

count page views

count page views

Share.

About Author

I'm Rajasekar - Web developer, Freelancer, Blogger and Owner of DeveloperDesks. From India lives in Bahrain. I love to do coding, Creating websites and trying different with code and designs. You Can Hire Me