How to Show PostViews without any Plugin in WordPress

Post Views is the number represents that the number a post have been viewed by visitors. You can find many plugin on the keyword ‘Post View’ in WordPress database. These plugins like WP-PostView offer lot of customization like showing top post, most viewed, top in a category and …. But if you don’t know much about WordPress coding you can hardly use the variable they declare and hence can’t show PostView at the right place you want. Moreover it is not good to use plugins for each & every needs. Uses of too much plugin can slow down your blog. So this tutorial is for beginner to show PostView without using any plugin at the right place they want.

Procedure of Showing PostViews:

  • First go to your Dashbord then open Appearance > Editor. Using this editor you can easily make change to any of your Template (Themes) files. So, before editing anyone please copy all the text code in a text document using Notepad or Word.
  • Select functions.php file at right side. It may be named different by your theme but the original name will also appear in a small text below the name. When the files appears in the editor copy all text and take a backup.
  • Then paste the code given below after the first <?php  but don’t put after ?> and then click Update.
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
return $count.' Views';
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
update_post_meta($postID, $count_key, $count);
  • Then open single.php file. This file is for showing single post. For counting PostViews of any post put the code given below at the top or beginning of the file and click Update

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

How to show Postviews in a Post:

To show PostViews number at any place of your post paste the code given below at that place of your single.php file

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

To show PostView beside title or date in a post open single.php file again and search in the document for something like posted on and paste after it. As I am using Mantra 1.9.2 at the time of writing this post the date code in my single.php file is <?php mantra_posted_on(); ?> and I paste the code after it. Then in a post, PostView shows like the image below. I selected don’t show post date from theme setting so date is not shown but category is shown and date position is also there.



But if you are not sure where the code locates there you want to show the views then put identification number like 11111, 22222, 33333 and so between the gap of each php script. A typical php script is <?php few_lines_of_code ?> . You can put those numbers between two php scripts like below


<div id="content" role="main">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
1111<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>2222
<h1><?php the_title(); ?></h1>3333
<div>4444 In Category:<?php mantra_posted_on();
 ?>5555 | <?php echo getPostViews(get_the_ID()); ?>6666
</div>7777<!-- .entry-meta --><div>
<div> <?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div><span>' . __( 'Pages:', 'mantra' ),
'after' => '</span></div>' ) ); ?>
</div><!-- .entry-content -->
<?php if ( get_the_author_meta( 'description' ) ) : // If a user has
filled out their description, show a bio on their entries  ?>


Now update it and load any post and you can see something like the image below



The numbers indicate different position in your post template. As I pasted the PostViews code at 6666, PostViews are shown there. I also putted a ‘|’ before it.

Note: Take a backup before making any changes, that is copy all text from the editor and save in a text file.


How to show Postviews on Home/Category/Archive Page:

To show PostViews on Home/Archive/Category Page open content.php file in the theme editor and paste <?php echo getPostViews(get_the_ID()); ?> at a place you desire. You can choose to show with date/category info. Use 1111, 2222, …. Method to determine the place.

Note: If newer version of your theme is available and you update your theme all the file will be replaced that is PostView will not be shown then. But PostView counts will still remain in your database. When you put those codes again in template file PostViews will be shown again. FIRST EDIT FUNCTIONS.PHP THEN SINGLE.PHP OR CONTENT.PHP OTHERWISE ERROR WILL OCCUR. On that case post will not be shown as functions are not declared so edit functions.php if you edited other ones first.

Tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.