Show Post View Count

Have you ever wanted to display a count of the current post’s views? Here’s a small code snippet that you can add to your site to accomplish this:

<?php
/**
 * @snippet       Post View Count
 * @author        Dave Jesch
 * @date-written  Mar 3, 2012
 * @date-revised  Feb 12, 2019
 * @testedwith    WordPress 5.0
 * @donate $5     https://davejesch.com/send-me-coffee/
 */

define( 'D3J_POST_VIEW_COUNT', '_d3j_post_view_count_meta' );

function d3j_get_post_views( $post_id )
{
	$count = abs( get_post_meta( $post_id, D3J_POST_VIEW_COUNT, TRUE ) );
	return sprintf( _n( '%s View', '%s Views', $count, 'text-domain' ),
		number_format_i18n( $count ) );
}

function d3j_increment_post_views( $post_id )
{
	if ( is_single() && ! is_user_logged_in() ) {
		$count = abs( get_post_meta( $post_id, D3J_POST_VIEW_COUNT, TRUE ) );
		if ( 0 === $count ) {
			add_post_meta( $post_id, D3J_POST_VIEW_COUNT, '1' );
		} else {
			update_post_meta( $post_id, D3J_POST_VIEW_COUNT, $count + 1, $count );
		}
	}
}

add_action( 'genesis_after_content', 'd3j_show_post_views', 20 );
add_action( 'astra_entry_bottom', 'd3j_show_post_views', 20 );
function d3j_show_post_views()
{
	$post_id = get_the_ID();
	d3j_increment_post_views( $post_id );
	echo '<div class="post-view-count">', d3j_get_post_views( $post_id ), '</div>';
}

In this example, I’m using the ‘genesis_after_content’ action to hook into a point within the Genesis Framework’s actions in order to display the view count after the post itself is displayed. If you’re using a different theme or Framework you will have to use the appropriate action hook for that Framework. Recently, I’ve updated the code to be compatible with the Astra theme, so you’ll also see a hook for ‘astra_entry_bottom’ . There’s no problem having multiple hooks set up for various themes- they will be safely ignored if the theme is not using them. Alternatively, you could use a filter on ‘the_content’ which would work for any theme or Framework.

A few things to note in the code:

First, I’m using a define called D3J_POST_VIEW_COUNT as opposed to a string, which I think most programmers would use. This is a habit from long ago. I’ve found that it’s easy to mistype a string. But if you’re using a define and you misspell it, you’ll get a PHP runtime error, instead of strange behavior. I would rather see an immediate error message, than just strange behavior. It’s faster in the development and debugging process. But if you don’t like to use a define like this you can just use the string instead.

Next, you’ll notice there is a CSS class name of “post-view-count” specified on the <div>. If you’re using my Site Functionality Plugin, you can add a rule in the css/site-functionality.css file. Below is an example of what this would look like to make the text bold, but you can make any styling you want.

.post-view-count {
  font-weight: bold;
}

Lastly, you’ll notice that I kept the two operations, incrementing the count and getting the count, as two separate functions. I think it’s always good to use small building blocks like this. Keeping them simple means that they’re easy to debug. Also, you may need the ability in the future to display the count in multiple places on the page. If that’s the case, you’d have to change the function’s behavior in order to avoid unwanted addition to the counts. With this implementation, you can call the d3j_show_post_views() function wherever you want to see the view count, as many times as you like within the template. If you think and build code in small building blocks like this, it makes things quite flexible.

Leave a Reply

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