WordPress Post View Count Without a Plugin

I will show you how many times you have watched your WordPress singles page and how to record it in the database. We can take and record in WordPress post_meta how many times you look at which page. We will make the function work by putting the following code in function.php page then we will trigger the function by putting the other code on a single.php page

 

function.php

 

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

single.php

 

setPostViews(get_the_ID());

 

3 Comments

  • zvodret iluret


    It’s laborious to search out knowledgeable individuals on this matter, however you sound like you recognize what you’re talking about! Thanks

    reply
  • Agresta


    Thank you for any other informative web site. Where else could I am getting that kind of info written in such an ideal manner? I have a project that I am just now working on, and I’ve been on the glance out for such info.

    reply

Leave a Reply

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