WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. MultiformeIngegno
    Member
    Posted 1 year ago #

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

    Thanks in advance! :D

    http://wordpress.org/extend/plugins/jetpack/

  2. MultiformeIngegno
    Member
    Posted 1 year 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>
    		<?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."

  3. Greg Ichneumon Brown
    Member
    Plugin Author

    Posted 1 year 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:

    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.

  4. MultiformeIngegno
    Member
    Posted 1 year 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:
    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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic