Как запросить количество просмотров для поста в Wordpress JetPack? - PullRequest
4 голосов
/ 28 марта 2012

Я использую статистику JetPack, чтобы следить за статистикой моего блога. Я хотел бы извлечь 10 самых популярных сообщений за определенный период (например, в прошлом месяце).

Я использовал API плагинов для статистики WordPress, который раньше работал хорошо, но после обновления до JetPack это больше не работает.

Существует ли API, который позволяет запрашивать количество просмотров для сообщения?

1 Ответ

6 голосов
/ 21 ноября 2012

Внутри базы данных

Эта опция записана в базе данных и используется виджетом Dashboard:

get_option( 'stats_cache' );

Возвращает массив, подобный этому:

array(
    ['7375996b7a989f95a6ed03ca7c899b1f'] => array(
        [1353440532] => array(
            [0] => array(
                ['post_id'] => 0
                ['post_title'] => 'Home page'
                ['post_permalink'] => 'http://www.example.com/'
                ['views'] => 1132
            )
            [1] => array(
                ['post_id'] => 4784
                ['post_title'] => 'Hello World!'
                ['post_permalink'] => 
                ['views'] => 493
            )
            /* till item [9] */

Опрос WordPress.com

Можно вызвать следующую функцию Jetpack:

if( function_exists( 'stats_get_csv' ) ) {
    $top_posts = stats_get_csv( 'postviews', 'period=month&limit=30' );
}

, которая возвращает:

array(
    [0] => array(
        ['post_id'] => 0
        ['post_title'] => 'Home page'
        ['post_permalink'] => 'http://www.example.com/'
        ['views'] => 6806
    )
    [1] => array(
        ['post_id'] => 8005
        ['post_title'] => 'Hello World!'
        ['post_permalink'] => 
        ['views'] => 1845
    )           
    /* till item [29] */

Функция get_stats_csv

/plugins/jetpack/modules/stats.php

Функция get_stats_csv вызывает http://stats.wordpress.com/csv.php.Если мы посетим этот адрес, мы получим ответ:

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.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...