How to display WordPress post view count without a plugin

Tutorial 1

Use this code in functions.php

Or use this code in your single.php

then use this code for display post counter

 

Tutorial 2

Add this code your functions.php

Add this code to your single.php file

Add this code for display post counter


  • Muthu Krishnan

    Hi Thanks for the code.it works

  • ady 1

    https://uploads.disquscdn.com/images/18732f413964dfa9b67e337a452c7b00c7c43886b88f598b54191c7637679b0a.jpg

    thank u Rubel miah for the code . it nice code and its work with me , but i have some problem and i hope to help me fix it .
    how to make this code give time per count view , and stop it to count administrator user .
    i found the code that gives time per count post view but this code count administrator user Also.
    are there way to make the code do both job .

  • masood

    hi rubel miah. i have to hide views count from post body, because its not showing exact count. let me know the way of hide it.

    • Hi Masood,

      Please give me your website link.

  • wiloke

    Awesome! But please note that, in case, if the website is using a caching plugin, wp_head will not work, We need to use ajax instead

  • Thanks for this great codes bro, it works good but that`s not counting true, increasing two by two. Why is it increasing two per one visit ?

    • Hi Halil,

      Can you share your website link.

      Thanks

      • Thanks for response bro 🙂
        yucetasarim.com/demo/supreme

        • It’s working perfectly on the single page. Please clean the browser cache then check.

          • Thanks bro, I cleared browser cache and closed the super cache plugin too. Tested it again but its happening same. When I enter to post its counting 1 view, its OK but when I went to other pages its counting 1 more too ?

          • You can try “Tutorial 2”, I hope it will worked.

          • I tried it too but same problem, its counting 2 by 2 🙁 I added codes single.php and other post type pages too. Is it problem ? For example Im adding codes to single-video.php too.

          • Yes, It’s Problem.

            Add this code in functions.php

            // function to display number of posts.
            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 to 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 to display number of video posts.
            function getVideoPostViews($postID){
            $count_key = ‘video_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 to count views.
            function setVideoPostViews($postID) {
            $count_key = ‘video_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);
            }
            }

            Add this code in single.php

            setPostViews(get_the_ID());

            And this code in single-video.php page.

            setVideoPostViews(get_the_ID());

            Add this code in Post meta area.

            echo getPostViews(get_the_ID());

            And this code in Video post meta area.

            echo getVideoPostViews(get_the_ID());

            Thanks.

          • thanks bro but sorry for bothering you, I made this too but same problem continues :/

          • Dreamelf

            Hello!

            I’m sorry about the bad English wording. That was the problem with me. At the post counter, it must be set to zero, and then it counts well, one at a time.

            https://uploads.disquscdn.com/images/f04d6125bc21dc06e88f8b27f02762850ad58ca26d988b0c719490110b70e347.jpg

  • I can’t post. This is my website: https://cybercoderbd.com