Popular posts by view with Jetpack

I’m using Jetpack to retrieve the number of views for each blog post.
Is there a way to use this to generate a call to the most viewed posts and list them?

I’ve been wanting to do that for a long time now and I’ve looked into various plugins, but I’d prefer to hardcode this. Is there a way to write a code so that I could utilize the view count provided by Jetpack?

I looked at similar questions here, but none of them has been answered. A few commenters suggested plugins, but those have been abandoned by now.
I couldn’t find any up-to-date information.

Right now I’m only calling the “view count” within my single.php like that:

<?php print_page_views(get_the_ID('')); ?>

For that I’m using a plugin called “Post ‘n Page Views” that requires Jetpack. I tried looking for the plugin in the WordPress Dabatase, but it doesn’t seem to exist anymore.

I really would appreciate any kind of advice or updated information on this.
Thank you so much!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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="%s" rel="nofollow noreferrer noopener">%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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x