You need to enable JavaScript to run this app. 如何实现WordPress文章接口端的浏览量统计与获取功能
优惠活动
大模型
产品
解决方案
定价
更多

如何实现WordPress文章接口端的浏览量统计与获取功能

Jetpack浏览量REST接口读取实现

Jetpack统计的文章浏览量不属于WordPress默认的post_meta字段,默认也不会暴露在文章REST接口的响应中,这也是很多人搜遍网络找不到可用读取函数的核心原因。

  • Jetpack插件在开启站点统计模块时,会内置全局读取函数jetpack_get_post_views($post_id),直接传入文章ID即可返回对应的浏览量数值
  • 千万别图省事直接查Jetpack的专属数据表取数:Jetpack不同版本的数据表结构、存储逻辑会调整,直接查表很容易出现兼容问题
  • 要让接口直接返回这个数值,需要通过rest_api_init钩子将字段注册到REST响应中
add_action('rest_api_init', 'register_jetpack_views_rest_field');
function register_jetpack_views_rest_field() {
    register_rest_field(
        'post',
        'jetpack_post_views',
        [
            'get_callback' => function($post_item) {
                // 兜底判断,Jetpack插件未激活时返回0,避免触发致命错误
                if(function_exists('jetpack_get_post_views')) {
                    return jetpack_get_post_views($post_item['id']);
                }
                return 0;
            },
            'schema' => [
                'description' => 'Jetpack统计的文章累计浏览量',
                'type' => 'integer'
            ]
        ]
    );
}

注意:如果是自定义文章类型,只需要把register_rest_field的第一个参数替换为对应文章类型的slug即可。

自研接口触发的浏览量统计方案

你之前找到的示例代码存在明显残缺:它只实现了读取计数并返回拼接文本的逻辑,既没有配置REST请求触发的累加入口,也没有将计数字段暴露到接口响应中,自然无法达到预期效果。
完整实现逻辑拆解:

  • 补全核心的计数累加函数,替代原代码的残缺逻辑
  • 匹配单篇文章的REST详情请求,触发时自动累加计数(排除文章列表接口,避免误触发)
  • 将自研统计的浏览量字段注册到REST响应中
// 浏览量累加核心函数
function increment_post_view_count($post_id) {
    // 校验文章合法性,避免无效ID触发无效数据库操作
    if(!get_post($post_id)) return;
    $meta_key = 'post_views_count';
    $current_count = get_post_meta($post_id, $meta_key, true);

    if($current_count === '') {
        // 首次访问时初始化计数为1
        add_post_meta($post_id, $meta_key, 1);
        return;
    }
    // 强转整数类型后累加,避免字符串拼接造成的计数异常
    update_post_meta($post_id, $meta_key, intval($current_count) + 1);
}

// 监听REST请求触发计数逻辑
add_action('rest_api_init', 'trigger_view_count_for_rest_request');
function trigger_view_count_for_rest_request() {
    $request_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    // 正则匹配单篇文章详情的REST路由,排除列表接口
    if(preg_match('#/wp-json/wp/v2/posts/(\d+)#', $request_path, $matches)) {
        $post_id = intval($matches[1]);
        // 基础防刷:同一客户端30分钟内访问同一篇文章不重复计数
        $cookie_identifier = 'wp_viewed_article_'.$post_id;
        if(!isset($_COOKIE[$cookie_identifier]) || (time() - intval($_COOKIE[$cookie_identifier])) > 1800) {
            increment_post_view_count($post_id);
            setcookie($cookie_identifier, time(), time() + 1800, '/');
        }
    }
}

// 将自研浏览量字段注册到REST响应
add_action('rest_api_init', 'register_custom_views_rest_field');
function register_custom_views_rest_field() {
    register_rest_field(
        'post',
        'custom_post_views',
        [
            'get_callback' => function($post_item) {
                return intval(get_post_meta($post_item['id'], 'post_views_count', true));
            },
            'schema' => [
                'description' => '自研逻辑统计的文章累计浏览量',
                'type' => 'integer'
            ]
        ]
    );
}

可扩展优化点:后续可以在计数触发逻辑中加入爬虫拦截、接口鉴权校验,避免机器人请求造成的虚高计数。


内容的提问来源于stack exchange,提问作者Samuel Edeh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.28 12:06:05