Display post views count with code, supporting cache plugins

post views in WordPress

I’ve found that most code snippets to display/track post views are just not working well if users are activating cache plugins like WP Super Cache on their sites.

The reality is that nowadays you are less likely to catch any website not using this kind of cache plugins for speed improvement.

So I’ve put together this post to show you how you are able to track post views count even with cache plugins enabled.

The first part would be the codes you see everywhere else. It’s working good without cache enabled.

I use the wp_head action hook so it executes the callback function whenever the page is loaded. Then I add some conditional statements to ensure the post views function is only proceeded when it’s in single post/page and the WP_CACHE is disabled.

The post views count is saved as a post meta with the key “views“. It automatically increases +1 whenever the page is loaded.

/**
 * Calculate Post Views with WP_CACHE disabled
 */
add_action( 'wp_head', 'process_post_views_without_wp_cache' );
function process_post_views_without_wp_cache() {
	global $post;

	if ( is_int( $post ) ) {
		$post = get_post( $post );
	}

	if ( wp_is_post_revision( $post ) || is_preview() ) {
		return;
	}

	if ( ! is_single() && ! is_page() ) {
		return;
	}

	if ( defined( 'WP_CACHE' ) && WP_CACHE ) {
		return;
	}

	$id = intval( $post->ID );
	$post_views = get_post_meta( $post->ID, 'views', true );
	if ( ! $post_views ) {
		$post_views = 0;
	}
	update_post_meta( $id, 'views', ( $post_views + 1 ) );
}

Above codes go to functions.php

The second part would be the codes particularly when cache is enabled.

Key concept here is that I’ll leverage AJAX to process the post views increment.

First I enquene the javascript file when it’s in single post/page and the WP_CACHE is enabled.

/**
 * Calculate Post Views with WP_CACHE enabled
 */
add_action( 'wp_enqueue_scripts', 'process_post_views_with_wp_cache_enabled' );
function process_post_views_with_wp_cache_enabled() {
	global $post;
	if ( is_int( $post ) ) {
		$post = get_post( $post );
	}

	if ( ! defined( 'WP_CACHE' ) || ! WP_CACHE ) {
		return;
	}

	if ( wp_is_post_revision( $post ) || is_preview() ) {
		return;
	}

	if ( ! is_single() && ! is_page() ) {
		return;
	}

	wp_enqueue_script( 'post-views-cache', get_template_directory_uri() . '/js/post-views-cache.js', array( 'jquery' ), '1.0', true );

	wp_localize_script( 'post-views-cache', 'PostViewsCache', array(
		'ajaxurl'  => admin_url( 'admin-ajax.php' ),
		'post_id'  => intval( $post->ID ),
	) );
}

Above codes go to functions.php

Then add below codes to the javascript file post-views-cache.js

jQuery.ajax({
	type: "GET",
	url: PostViewsCache.ajaxurl,
	data: {
		"post_id": PostViewsCache.post_id,
		"action": "process_postviews"
	},
	cache:!1,
	success: function(data) {
		console.log("New PostViews: "+data);
	}
});

The action process_postviews should be attached to our ajax handler as blow.

// Ajax handler to process post views with WP_CACHE enabled
add_action( 'wp_ajax_process_postviews', 'process_postviews' );
add_action( 'wp_ajax_nopriv_process_postviews', 'process_postviews' );
function process_postviews() {
	if ( ! defined( 'WP_CACHE' ) || ! WP_CACHE ) {
		return;
	}

	if ( empty( $_GET['post_id'] ) ) {
		return;
	}

	$post_id = intval( $_GET['post_id'] );
	if ( $post_id > 0 ) {
		$post_views = get_post_meta( $post_id, 'views', true );
		if ( ! $post_views ) {
			$post_views = 0;
		}
		update_post_meta( $post_id, 'views', ( $post_views + 1 ) );
		echo ( $post_views + 1 );
	}
	exit();
}

Above codes go to functions.php

Then we’re all set and ready to get the views count.

Usage example (in the loop).

$post_views = get_post_meta( get_the_ID(), 'views', true );
if ( ! $post_views ) {
    $post_views = 0;
}
echo $post_views;
Save

Author: Leo

My name is Leo who's behind this blog. I've been working with wordpress since 2010. I'll write about themes, plugins, tutorials and all about wordpress here. Hope you find it useful!

2 thoughts on “Display post views count with code, supporting cache plugins”

Leave a Reply

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