I have this code for count the views of page :
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
function PostViews($postID) {
$key = 'post_views_count';
$count = get_post_meta($postID, $key, true);
if($count==''){
$count = 1;
delete_post_meta($postID, $key);
add_post_meta($postID, $key, '1');
return $count;
}else{
$count++;
update_post_meta($postID, $key, $count);
return $count;
}
}
the problem is when i visit the page every time add one view , how to ignore the same IP from added again ?
Save the user’s IP address into an array. Save the
JSON
encoded version of the array into the database then decode and loop through it to match the IP addresses.Hope that helps!
Some little improvement to the script above.
Add IP anonymously with wp_hash() and a count based on array count.