Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am having a small issue to display most views post in my blog where it is incrementing twice for each visit of post. I am not sure why this is happening because i didn't put any code to single.php file. Just follow the below rules & code where passing a tracker function to wp_head:

function cosonix_set_post_views($postID) {
  $count_key = 'count_post_views';
  $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= $count+1;
     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);

//adding tracker function for wp_head 
function track_post_views () {
    if ( is_single() && !is_page() ) {
        global $post;
        cosonix_set_post_views($post->ID);
    } else { 
        return ;
    }
}

add_action( 'wp_head', 'track_post_views');

function cosonix_get_post_views($postID){
     $count_key = 'count_post_views';              
     $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";
     }
     return $count;
}

Anybody please help me to solve where I am getting wrong?

share|improve this question
    
What happens when you exclude prefetchers? –  toscho Mar 5 at 22:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.