Popular posts by view with Jetpack

There is a Jetpack widget called Top Posts and Pages (Jetpack)

enter image description here

If you check out the [source code][2] for this widget, you can see that it’s using the function stats_get_csv() to retrieve the stats:

$post_view_posts = stats_get_csv( 'postviews', array( 'days' => 2, 'limit' => 10 ) );

If you want to generate your custom most popular list, you can use for example:

if(function_exists('stats_get_csv')){
        $popular = stats_get_csv( 'postviews', array( 'days' => 2, 'limit' => 10 ) );
        echo '<ol>';
        foreach ( $popular as $p ) {
                printf('<li><a href="https://wordpress.stackexchange.com/questions/95847/%s">%s</a>(%d)</li>', $p['post_permalink'], $p['post_title'], $p['views'] );
        }
        echo '</ol>';
} 

The function stats_get_csv( $table, $args = null ) is defined in:

http://plugins.trac.wordpress.org/browser/jetpack/tags/2.2.6/modules/stats.php

where the data is fetched from

http://stats.wordpress.com/csv.php

Note that stats_get_csv is caching the data for 5 minutes.

For an example of the what the stats_get_csv outputs and the API description, please check out this great answer.

Leave a Comment