How to Display Total Views on WordPress Posts

If a tree falls in a forest and no one is around to hear it, does it make a sound? Likewise, if a page on your blog is popular but no one can see the view count, does it even matter that it's popular?

Don't worry – it's easy to display how many times your WordPress posts have been viewed. There are two methods for doing this. You can use a plugin, or you can add the necessary code manually.


Displaying Post Views With A Plugin

There are many plugins to choose from for displaying view counts. We recommend using one of these three:


#1 Post Views Counter

With its 4.9/5 rating, Post Views Counter is one of the most well-regarded WordPress view counters. It's fast, and it has a lot of features, including a sortable admin column, custom REST API endpoints, W3 Cache compatibility, and one-click data import from WP-PostViews.

Also, you can exclude counts from certain users. This is quite useful, as the whole point of counting views is to monitor the behavior of visitors, so you don't want hits from yourself or your staff to add to the total.

But if you don't care about accuracy, Post Views Counter gives you the ability to manually set the view count. You can fudge the numbers to make a post look more or less popular than it really is.


#2 Simple Post Views Counter

Simple Post Views Counter isn't as feature-rich as Post Views Counter, but as the name suggests, it's very easy to use. If you just want to slap a view counter on your posts and forget about it, this is the plugin for you.


#3 WP-PostViews

WP-PostViews has been installed over 200,000 times, making it one of the most popular WordPress view counters.

To display the total views of a post, add this code to your site’s single.php template:

<?php if(function_exists('the_views')) { the_views(); } ?>

After adding the code, you can control who sees the view counts on your posts by going to the built-in control panel and selecting your preferred option in the ‘Count Views From' tab.


Displaying Post Views Without A Plugin

Using a plugin is the most convenient way to display view counts, but adding the code yourself is a more permanent solution, and it takes up less space.

It's not as difficult as you may think. First, go to ‘Appearance' and select the ‘Editor' option. Then, open function.php (you can find it in the list of PHP files on the right side of the screen) and add the following code:

// Display or Count how many times a post has been viewed. // id = the post id and action = display or count function arixWp_PostViews( $id, $action ) { $axCountMeta = 'ax_post_views'; // Your Custom field that stores the views $axCount = get_post_meta($id, $axCountMeta, true); if ( $axCount == '' ) { if ( $action == 'count' ) { $axCount = 0; } delete_post_meta( $id, $axCountMeta ); add_post_meta( $id, $axCountMeta, 0 ); if ( $action == 'display' ) { echo "0 Views"; } } else { if ( $action == 'count' ) { $axCount++; update_post_meta( $id, $axCountMeta, $axCount ); } else { echo $axCount . ' Views'; } } }

Select ‘Update File'. After function.php is updated, open single.php from that same list of PHP files and add the following code after <div class=”entry”>:

<?php echo arixWp_PostViews( get_the_ID(), 'count' ); ?>

Use this code to display the post count outside the loop:

<?php echo arixWp_PostViews( get_the_ID(), 'display' ); ?>

Use this code to display the post count inside the loop:

<?php echo arixWp_PostViews( $post->ID, 'display' ); ?>

Finally, select “Update File”. That should do it.

How to Display Total Views on WordPress Posts
3.8 (75%) 4 votes