如何在 Wordpress JetPack 中查询帖子的查看次数? [英] How to query the number of view counts for a post in Wordpress JetPack?

查看:24
本文介绍了如何在 Wordpress JetPack 中查询帖子的查看次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 JetPack 统计信息来跟踪我博客的统计信息.我想提取给定时间段(例如上个月)内观看次数最多的前 10 个帖子.

I use JetPack stats to follow the stats for my blog. I would like to extract the top 10 most-viewed posts for a given period (e.g. last month).

我之前使用过 WordPress stats 插件 API,该 API 运行良好,但升级到 JetPack 后就不再适用了.

I used the WordPress stats plugin API before which worked nicely, but after upgrade to JetPack this doesn't work anymore.

是否有允许我查询帖子查看次数的 API?

Is there an API which allows me to query the number of view counts for a post?

推荐答案

Inside the Database

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

Inside the Database

This option is recorded in the database and is used by the Dashboard widget:

get_option( 'stats_cache' );

它返回一个这样的数组:

It returns an array like this:

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 函数:

Querying WordPress.com

It is possible to call the following Jetpack function:

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.如果我们访问这个地址,我们会得到这样的回应:

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.

这篇关于如何在 Wordpress JetPack 中查询帖子的查看次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆