Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

1 Answer

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

enter image description here

If you check out the source code 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 ) );

Here is an example of the array output:

Array
(
    [0] => Array
        (
            [post_id] => 0
            [post_title] => Home page
            [post_permalink] => http://example.com/
            [views] => 150
        )

    [1] => Array
        (
            [post_id] => 1
            [post_title] => Hello World!
            [post_permalink] => http://example.com/hello-world/
            [views] => 50
        )

     ...
)

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.2b8/modules/stats.php#L995

where the data is fetched from

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

which has the following API description:

Required parameters: api_key, blog_id or blog_uri.
Optional parameters: table, post_id, end, days, limit, summarize.

Parameters:
api_key     String    A secret unique to your WordPress.com user account.
blog_id     Integer   The number that identifies your blog. Find it in other stats URLs.
blog_uri    String    The full URL to the root directory of your blog. Including the full path.
table       String    One of views, postviews, referrers, referrers_grouped, searchterms, clicks, videoplays.
post_id     Integer   For use with postviews table. Also accepts a comma-separated list of integers.
end         String    The last day of the desired time frame. Format is 'Y-m-d' (e.g. 2007-05-01) and default is UTC date.
days        Integer   The length of the desired time frame. Default is 30. "-1" means unlimited.
period      String    For use with views table and the 'days' parameter. The desired time period grouping. 'week' or 'month'
                      Use 'days' as the number of results to return (e.g. '&period=week&days=12' to return 12 weeks)
limit       Integer   The maximum number of records to return. Default is 100. "-1" means unlimited. If days is -1, limit is capped at 500.
summarize   Flag      If present, summarizes all matching records.
format      String    The format the data is returned in, 'csv', 'xml' or 'json'. Default is 'csv'.

Non-working query example: ?api_key=123456789abc&blog_id=155&table=referrers&days=30&limit=-1&summarize

Result format is csv with one row per line and column names in first row.

Strings containing double quotes, commas, or "\n" are enclosed in double-quotes. Double-qoutes in strings are escaped by inserting another double-quote.
Example: "pet food" recipe
Becomes: """pet food"" recipe"

Developers, please cache the results for at least 180 seconds.

stats_get_csv is caching the data for 5 minutes.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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