Support » Plugin: Jetpack by » Function to retrieve most viewed posts via Jetpack stats

Viewing 3 replies - 1 through 3 (of 3 total)
  • In jetpack/modules/stats.php I found this:

    <div id="top-posts" class='stats-section'>
    		<div class="stats-section-inner">
    		<h4 class="heading"><?php _e( 'Top Posts' , 'jetpack' ); ?></h4>
    		if ( empty( $top_posts ) ) {
    			<p class="nothing"><?php _e( 'Sorry, nothing to report.', 'jetpack' ); ?></p>
    		} else {
    			foreach ( $top_posts as $post ) {
    				if ( !get_post( $post['post_id'] ) )
    				<p><?php printf(
    					'<a href="' . get_permalink( $post['post_id'] ) . '">' . get_the_title( $post['post_id'] ) . '</a>',
    					number_format_i18n( $post['views'] )
    				); ?></p>

    but it needs to be put in context.. because if I put this in a custom template I simply get “Sorry, nothing to report.”

    Plugin Contributor Greg Ichneumon Brown


    Hi MultiformeIngegno,

    Unfortunately there is a not a spectacular way to do this mostly because it could significantly slow down your page load time.

    The function you see there does get the top posts for display in the admin by downloading a csv of our stats from

    Getting that csv can take quite a while, and the more traffic you have, the longer it will take. So doing this for every page load would not be something I would recommend unless you have some good local caching of the data and are only loading it once a day or so.

    You could certainly reuse some of the above code to be able to do that, and could probably get it to work. But you should be careful in doing so. 🙂

    Hope that helps.

    Yeah, after digging for hours around the Internet I managed to create a snippet that works (approximately). The problem is that it has the same issue of this:

    It doesn’t respect the “limit” I set, and when I set a greater number in “days” parameters, the number of posts shown is less (when I set it to -1 only 1 post is shown)! 🙁
    Do you know why?

    P.S.: here’s the snippet I use:

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Function to retrieve most viewed posts via Jetpack stats’ is closed to new replies.