Popular posts reset count (PHP)

I’m a website owner and a beginner web developer, my knowledge so far sums in HTML, CSS and JavaScript.
However, for my website daily use I have found and implemented the following PHP code:

// 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');

It works pretty great, however I want to make the count reset automatically every week, and start counting from zero again.

Read More

Can someone please help me achieve that? (unfortunately google couldn’t help with this one…)

P.S. I don’t want to use a plugin!

Related posts

Leave a Reply

1 comment

  1.     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'));
        }
    }
    

    Try this working or not.