wordpress

WordPress post view count without plugin

In this article, we will learn about how we can count wordpress post view count without using any plugin. As you know, the less plugin you use the faster your website, and I think to count post view in wordpress doesn’t need any plugin to do that.

We will follow below steps in orders to add wordpress post view count on blog post.

  • Install WordPress
  • Change in Function.php file
  • Change in single.php file
  • Change in content-single.php

Step – 1 Install WordPress

To install WordPress in your local system then please watch the below video.

Step – 2 Change in function file

To make a change in your theme function file, then logged in to your WordPress admin panel, then look for appearance on the sidebar, and then click on theme editor, you can see something like the below image.

theme-editor-change-post-count
Theme Editor Page

Now on the left side you can Theme FIles, from there choose function.php file as like the below image.

Functions.php

Now in function.php file add the below code.Add this code after other code ends in function.php file

//page views function
 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";
     }
     return $count;
 }
 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);

Now look for single.php file as like the below image put the code just after your endwhile loop

singles.php-file
Singles.php File

Add the below code in the singles.php file

//page view count
setPostViews(get_the_ID());

Now I am going to show page view count on the top of my post for that I will use the below code. Now go to template-parts/content-single.php, please refer the image

template-parts/content-single.php

In content-single.php add the below code, where ever you want to show the page view count on your blog post I have added on the top of my post.

     <h3 class="entry-date published" style="color:grey;">Post View Count : <?php echo getPostViews(get_the_ID()); ?> </h3>

The below image is an example of the code explained in this blog.

post-view-count-in-the-wordpress-post-page
Post view count in the wordpress post page

I hope you like my post, feel free to ask the question if you stuck anywhere by doing this.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

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

Back to top button