How to display WordPress post view count without a plugin

Tutorial 1

Use this code in functions.php

function rm_post_view_count(){
	if ( is_single() ){
		global $post;
		$count_post = esc_attr( get_post_meta( $post->ID, '_post_views_count', true) );
		if( $count_post == ''){
			$count_post = 1;
			add_post_meta( $post->ID, '_post_views_count', $count_post);
		}else{
			$count_post = (int)$count_post + 1;
			update_post_meta( $post->ID, '_post_views_count', $count_post);
		}
	}
}
add_action('wp_head', 'rm_post_view_count');

Or use this code in your single.php

if ( is_single() ){
		global $post;
		$count_post = esc_attr( get_post_meta( $post->ID, '_post_views_count', true) );
		if( $count_post == ''){
			$count_post = 1;
			add_post_meta( $post->ID, '_post_views_count', $count_post);
		}else{
			$count_post = (int)$count_post + 1;
			update_post_meta( $post->ID, '_post_views_count', $count_post);
		}
	}

then use this code for display post counter

global $post;
$visitor_count = get_post_meta( $post->ID, '_post_views_count', true);
if( $visitor_count == '' ){ $visitor_count = 0; }
if( $visitor_count >= 1000 ){
	$visitor_count = round( ($visitor_count/1000), 2 );
	$visitor_count = $visitor_count.'k';
}
echo esc_attr($visitor_count);

 

Tutorial 2

Add this code your 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);
    }
}

Add this code to your single.php file

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

Add this code for display post counter

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

28 Comments

  • Muthu Krishnan

    July 14, 2017

    Hi Thanks for the code.it works

  • ady 1

    July 23, 2017

    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

    September 7, 2017

    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.

  • wiloke

    September 16, 2017

    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

  • Halil ibrahim YÜCE

    October 12, 2017

    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 ?

    • Rubel Miah

      October 24, 2017

      Hi Halil,

      Can you share your website link.

      Thanks

      • Halil ibrahim YÜCE

        October 24, 2017

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

        • Rubel Miah

          October 24, 2017

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

          • Halil ibrahim YÜCE

            October 24, 2017

            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 ?

          • Rubel Miah

            October 28, 2017

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

          • Halil ibrahim YÜCE

            October 28, 2017

            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.

          • Rubel Miah

            October 29, 2017

            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.

          • Halil ibrahim YÜCE

            October 29, 2017

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

          • Dreamelf

            December 1, 2017

            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

  • Md Al-Amin Ahmed

    October 22, 2017

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

  • Keyur Verma

    December 17, 2017

    nice and good : http://thekipkopmusic.com/

Leave a Reply