You are a content creator, and you create posts. Now you need to count post views and display them to viewers in WordPress. The easiest way to count such post views is to incorporate the below-given code in any WordPress theme so that your viewers can easily know the number of views on each blog post on your WordPress website. Using this code, readers can determine the popularity of the article with a higher number of views, and they will feel more secure. Hence, I want to click to view the entire blog post when the number of views is higher than the others. So, in this article, we will teach you how to count post views and display them in WordPress without using plugins. 

Step1. Add post views function

Activate your theme. Go to appearance >> Theme Editor. Now in left hand side, find function.php file and add following code in it . This is a function that initiated for counting post views. Here you can use your own function name instead of ‘ function_name_given ‘ . We recommend to use theme prefix as well with function name such as theme_name_function_name_given .

if ( ! function_exists( 'function_name_given' ) ) :    
/**     * get the value of view.     */ 
function function_name_given($postID) {   
$count_key = 'wpb_post_views_count';    
$count = get_post_meta($postID, $count_key, true);    
if($count ==''){        
$count = 1;        
delete_post_meta($postID, $count_key);        
add_post_meta($postID, $count_key, '1');    
} else {        
update_post_meta($postID, $count_key, $count);    

Step2. Add post views count

Now similarly add following code in single.php but before endwhile loop. This code will count the number of views with the help of above function in a visited post.

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

Step3. Display count post views

After getting count of post views , it should be displayed where ever you like to display using following code such as in index.php or in single.php itself. You can use Div instead of Li as per your requirement in your WordPress website. You can change the icon “fa fa-eye” as you like from fontawesome.

<li > <i class="fa fa-eye"></i>                            
if ( get_post_meta( get_the_ID() , 'wpb_post_views_count', true) == '') {                                 echo '0' ;                            
} else { 
echo get_post_meta( get_the_ID() , 'wpb_post_views_count', true); };                            ?>                             

Below is the example from pro voice blog wordpress theme.

This feature is available in many of our premium themes. Hence this code is truly and efficiently works.

count post views


If you are able to implement above codes in your theme, you will definitely count and display your blog post count without using any plugins. If need support then you can contact us. We will here to support you. Meanwhile you can see some of our cool free themes here.


Leave a Reply

Share via
Copy link