I have a simple function that should add 1 to the page views in the wp_postmeta table every time a user visits the page. The problem is that in chrome, the number increments by two each time the page is visited. I had initially done this on a taxonomy page and the number was actually incrementing by four on each visit. The code I used is below and comes from http://wpsnipp.com/index.php/functions-php/track-post-views-without-a-plugin-using-post-meta/
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
function setPostViews($postID) {
$count_key = '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);
}
}
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
setPostViews($postID)
is not inside a loop of any kind. – henrywright Apr 11 '14 at 0:15