WordPress.org

Support

Support » Plugins and Hacks » Jetpack by WordPress.com » Function to retrieve most viewed posts via Jetpack stats

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>
    		<?php
    		if ( empty( $top_posts ) ) {
    			?>
    			<p class="nothing"><?php _e( 'Sorry, nothing to report.', 'jetpack' ); ?></p>
    			<?php
    		} else {
    			foreach ( $top_posts as $post ) {
    				if ( !get_post( $post['post_id'] ) )
    					continue;
    				?>
    				<p><?php printf(
    					$printf,
    					'<a href="' . get_permalink( $post['post_id'] ) . '">' . get_the_title( $post['post_id'] ) . '</a>',
    					number_format_i18n( $post['views'] )
    				); ?></p>
    				<?php
    			}
    		}
    		?>
    		</div>
    	</div>

    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

    @gibrown

    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 wordpress.com:

    http://plugins.trac.wordpress.org/browser/jetpack/trunk/modules/stats.php#L955

    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:
    http://wordpress.org/support/topic/magic-number-isnt-magic?replies=1

    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: http://wordpress.stackexchange.com/questions/71539/stats-get-csv-retrieve-just-a-few-posts-not-the-one-i-set-in-limit-parameter

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.
Skip to toolbar