Jetpack by WordPress.com
Function to retrieve most viewed posts via Jetpack stats (4 posts)

  1. MultiformeIngegno
    Posted 2 years ago #

    Hi guys! :)
    I'm looking for a function to retrieve the X most viewed posts through Jetpack (integrated) stats.

    Thanks in advance! :D


  2. MultiformeIngegno
    Posted 2 years ago #

    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."

  3. Greg Ichneumon Brown
    Plugin Contributor

    Posted 2 years ago #

    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:


    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.

  4. MultiformeIngegno
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Jetpack by WordPress.com
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic