Monday, 15 July 2013

wordpress - Popular posts reset count (PHP) -



wordpress - Popular posts reset count (PHP) -

i'm website owner , beginner web developer, knowledge far sums in html, css , javascript. however, website daily utilize have found , implemented next 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 maintain count accurate, lets 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, want create count reset automatically every week, , start counting 0 again.

can please help me accomplish that? (unfortunately google couldn't help one...)

p.s. don't want utilize plugin!

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 working or not.

php wordpress

No comments:

Post a Comment