How to display Jetpack stats per post in WordPress

- by

Some websites employ this or similar technologies to show how many views a single post has had. I was wondering how they did that without starting to count stats that have already been counted for several years, either by Google or by Jetpack.

Yesterday I came across this post by a WordPress dev named Topher about how to render Jetpack Stats: http://wpgr.org/2013/03/02/rendering-jetpack-stats/

I decided to test this in TwentyThirteen, and it works a treat – here’s how to do it. The principle will of course work with any theme.

Adding code to functions.php

We need to add a function to our theme’s functions.php file. This will use Jetpack’s stats_get_csv function and query how many views the supplied post ID had.

// extract JetPack pageviews
// courtesy of Topher: http://wpgr.org/2013/03/02/rendering-jetpack-stats/

function get_page_views($post_id) {

	if (function_exists('stats_get_csv')) {
	
		$args = array('days'=>-1, 'limit'=>-1, 'post_id'=>$post_id);
		$result = stats_get_csv('postviews', $args);
		$views = $result[0]['views'];

	} else {

		$views = 0;

	}
	return number_format_i18n($views);
}

Displaying the stats

Now we can call this function from anywhere in our theme. Perhaps it makes the most sense to show it alongside all the other meta data (i.e. next to tags, categories, date and author information).

In TwentyThirteen this is pulled in via a template part called content.php. Other themes may not use a template part and will print the post title and meta information as part of index.php (for the front page), or in single.php (for a single post). You probably know your theme better than I ever will.

Pick a place you’d like your stats to appear. In TwentyThirteen it’s done via a function called entry-meta. Right underneath, I’ll add my own code:


First I extract the current post ID, then I call our function above with the post ID, and store its output in a variable. On the last line I’ll simply print it out. Add a class to it and make it look handsome in CSS if you like.

The first time we call out method, it will take a moment longer to load the website. Subsequent executions will be faster. Caching is a wise choice here.

The result looks something like this:

Screen Shot 2015-11-21 at 09.52.53

Too much hacking… isn’t there a plugin I can use?

Of course there is, it’s called Jetpack Post Views: https://wordpress.org/plugins/jetpack-post-views/developers/

Sadly it’s no longer maintained by its developer Steven Lambert, but it still works and is based on the same principle.

To achieve the same principle without relying on Jetpack, have a look at Lester Chan’s WP PostViews plugin: https://wordpress.org/plugins/wp-postviews/



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

8 thoughts on “How to display Jetpack stats per post in WordPress”

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.