wordpress - 如何在 Wordpress JetPack 中查询帖子的浏览次数?

标签 wordpress statistics

我使用 JetPack 统计信息来跟踪我的博客的统计信息。我想提取给定时期(例如上个月)查看次数最多的 10 个帖子。

我之前使用过 WordPress 统计插件 API,效果很好,但升级到 JetPack 后就不再起作用了。

是否有 API 可以让我查询帖子的浏览次数?

最佳答案

数据库内部

此选项记录在数据库中并由仪表板小部件使用:

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.

关于wordpress - 如何在 Wordpress JetPack 中查询帖子的浏览次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9910791/

相关文章:

php - Wordpress PHP 错误标签包装和 css 样式?

php - 切换订阅 - 直接添加到购物车

css - 如何覆盖从外部小部件生成的 CSS?

jquery - WordPress Jquery 与插件冲突

algorithm - 如何操纵围绕中心值震荡的价格序列(指标)?

php - 获取 woocommerce 产品标签 slugs 作为数组

python - 定量数据集的显着性检验(Python Pandas)

python - 在 scipy 中基于频率数据有效拟合分布

python - 什么是 pm.Normal.dist.logp?

perl - 是否有一个 Perl 统计包不能让我一次加载整个数据集?