Count post views without wordpress plugin using meta data

Count post views without wordpress plugin using meta data

Showing post views is nice feature. Many client want to show post/page views on pages. Where in this wordpress tutorial, we will show, How to show post views using post meta data.

There are many wordpress plugins available for counting post views. Here are some, If you don’t want to modify your wordpress theme code.

Count post views without wordpress plugin using meta data
Count post views without wordpress plugin using meta data

Automated method

Add following wordpress plugin and show post views

WP-PostViews

Enables you to display how many times a post/page had been viewed.

Post Views Counter

Post Views Counter allows you to display how many times a post, page or custom post type had been viewed with this simple, fast and easy to use plugin.

For more information, check out plugin page at dFactory or plugin support forum.

Features include:

  • Option to select post types for which post views will be counted and displayed.
  • 2 methods of collecting post views data: PHP and Javascript, for greater flexibility
  • Option to set time between counts
  • Excluding counts from visitors: bots, logged in users, selected user roles
  • Excluding users by IPs
  • Restricting display by user roles
  • One-click data import from WP-PostViews
  • Post views display position, automatic or manual via shortcode
  • W3 Cache/WP SuperCache compatible
  • WPML and Polylang compatible
  • .pot file for translations included

Manual Method

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

Add above code into functions.php file which you can find in theme folder.

Step 1
After this you need put following in single.php file and page.php file. Inside post loop, copy and paste following code.

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

Note: If you put above code in wrong place than it will not work.

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.