So i have this function for my Wordpress site which basically just counts every click and shows the most popular ones in a loop. My problem is that the meta fields doesn't reset or decrease after time. So it will show old posts instead of new ones. I don't have that much knowlege in PHP so if anyone can help me, that will be greatly appreciated!
This is my current PHP function:
function shapeSpace_popular_posts($post_id) {
$count_key = 'popular_posts';
$count = get_post_meta($post_id, $count_key, true);
if ($count == '') {
$count = 0;
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '0');
} else {
$count++;
update_post_meta($post_id, $count_key, $count);
}
}
add_action('wp_ajax_track_clicks', 'track_clicks');
add_action('wp_ajax_nopriv_track_clicks', 'track_clicks');
function track_clicks(){
$post_id = $_POST['post_id'];
shapeSpace_popular_posts($post_id);
echo 'Success';
wp_die();
}