【发布时间】:2017-11-29 18:10:05
【问题描述】:
我正在尝试创建一个函数,该函数仅从过去一周的帖子中获取总观看次数。目前该功能正在获取所有时间视图的总数。我基本上希望它在每周开始时重置为 0。这可能吗?我怎么能这样做?
// Popular Posts Week
function wpb_set_post_viewss($postID) {
$count_key = 'wpb_post_views_counts';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
//To keep the count accurate, lets get rid of prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
function wpb_track_post_viewss ($post_id) {
if ( !is_single() ) return;
if ( empty ( $post_id) ) {
global $post;
$post_id = $post->ID;
}
wpb_set_post_viewss($post_id);
}
add_action( 'wp_head', 'wpb_track_post_viewss');
【问题讨论】:
-
如果我要解决这个问题,我通常会使用两种方法。如果我自己跟踪它,我可以使用 sql 选择上周数据库中的所有视图。第二种更简单的方法是调用谷歌分析 api 并以这种方式获取。
-
@Chris,我该怎么做?
标签: javascript jquery wordpress function