1

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!

1 Answer 1

10

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

5
  • Pretty much the same as here stackoverflow.com/a/13481345/2589071 Commented Jun 4, 2014 at 15:53
  • Thanks for the link, yes I agree, it looks similar. I think everyone trying to understand this function will go through similar process: use print_r to investigate the output, check out JetPack source code and view the api docs. When I work on questions I experiment with my own dev install and sometimes use print_r to get array outputs. I usually replace my own site url with example.com, which I use a lot in my answers. The text "Home page" was added by JetPack. When checking out the source of the HTML pages in Chrome, there are no quotes around text, so my output doesn't have them.
    – birgire
    Commented Jun 4, 2014 at 17:04
  • I thought you could use post_views_count in a WP_Query? Commented Jun 4, 2014 at 21:32
  • That's interesting, does the Jetpack stats module use the post_views_count meta key? I must have missed that ;-) I tried some keyword searches on the JetPack code base but I couldn't find it. But I've seen this method used without JetPack, to increment some post meta value on each page load. But it needs to be ajax based to handle cache plugins. Then it's handy to use WP_Query to display such a list.
    – birgire
    Commented Jun 4, 2014 at 21:44
  • No but there's several sites saying it can used in a query without a function but i doubt it. Commented Jun 4, 2014 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.