热门帖子重置计数(PHP)

时间:2014-06-24 09:41:00

标签: php wordpress

我是网站所有者和初学者网络开发人员,到目前为止,我的知识总结为HTML,CSS和JavaScript。 但是,对于我的网站日常使用,我发现并实现了以下PHP代码:

// Popular Posts
function wpb_set_post_views($postID) {
        $count_key = 'wpb_post_views_count';
        $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_views ($post_id) {
    if ( !is_single() ) return;
    if ( empty ( $post_id) ) {
        global $post;
        $post_id = $post->ID;    
    }
    wpb_set_post_views($post_id);
}
add_action( 'wp_head', 'wpb_track_post_views');

它工作得非常好,但我希望每周自动重置计数,并从零开始计数。

有人可以帮我实现吗? (不幸的是谷歌无法帮助解决这个问题......)

P.S。我不想使用插件!

1 个答案:

答案 0 :(得分:0)

    function wpb_set_post_views($postID) {
    $count_key = 'wpb_post_views_count';


    $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);
    }

    $timeStamp_key = 'wpb_post_views_timestemp';

    $lastDate = date('d',strtotime("",get_post_meta($postID,$timeStamp_key,true)));
    $currentDate = date("d",strtotime('-7 Days',time()));

    if($lastDate == $currentDate) {
        update_post_meta($postID, $count_key, '0');
        update_post_meta($postID,$timeStamp_key,date('Y-m-d H:i:s'));
    }
}

尝试这项工作与否。