How to display Jetpack stats per post in WordPress

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 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:

<div class="entry-meta">
    <?php twentythirteen_entry_meta(); ?>
                
                <?php 
                // adding page views after Meta Information
                $post_id = get_the_ID(); 
                $pageviews = get_page_views($post_id); 
                echo "$pageviews Views."; 
                // end of views
                ?>

    <?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-meta -->

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/





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.