2

I'd like to create a query to show the top 6 most read posts over the last two days. However, I'd like to only include posts created within the last month to prevent older stories from showing up.

I am using Jetpack and wordpress.com stats, so I've created this query for Top Posts, but I'm unsure whether it is the correct approach

$top_posts = stats_get_csv('postviews', 'period=month&days=2&limit=6')

My primary confusion is with the stats_get_csv API period parameter, and how it is used / what the effects are.

1 Answer 1

1

While researching to answer this SO Question, stumbled upon this one, which gave me the final hint.

I'll reproduce the relevant part:

Function get_stats_csv

/plugins/jetpack/modules/stats.php

The function get_stats_csv calls http://stats.wordpress.com/csv.php. If we visit this address, we get this response:

Error: api_key is a required parameter.

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

For what I understood of this documentation, using period=week&days=1&limit=-1' will return all posts from 1 week period. If we use days=2 it will be a 2 weeks period.

One week seems to be the shortest period. You would have to compare Jetpack results with your internal query of only two days and filter the results.

Take that developers note seriously, as it looks I broke my daily (hopefully) quota while testing. The function is not returning results anymore and even preventing my code from running... Nothing breaks, but simply don't run.

1
  • The way I read your question though, you're interested only in posts created for some time period. Jetpack is unaware of publish dates, so while you will get pageview data for the defined time period, those pageviews may very well be for posts created outside of the defined time period. I've done something similar and used a custom loop to fill an array with posts published in my time period, then check my array against the array returned by jetpack.
    – Will
    Commented Dec 29, 2013 at 8:51

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.