views:

48

answers:

1

Hello,sorry for starting another post,here is the link of my previous similar post my previous question.Problem is when someone visit some post,I want to record that,to increment counter for that post.But counter is also increment for previous post(always previous post).Can not find a way to solve this.

here is the code that I use :

function IncrementPostCount(){
  if(is_single()){
    global $wp_query;
    $count = get_post_meta( $wp_query->post->ID, 'readnTimes', true );
    $count = empty($count) ? 1 : $count + 1;
    add_post_meta($wp_query->post->ID, 'readnTimes', $count, true) or update_post_meta($wp_query->post->ID, 'readnTimes', $count);
  }
}
add_action( 'template_redirect', 'IncrementPostCount' );

Can someone help me with this please.

+1  A: 

This function already exists in various plugins. For example the "Post Views" plugin

Benoit Courtine
I think I know where the problem is,I didnt check this function wp_is_post_revision,it gets real post ID,I will try,t u very much.
Still can't get this thing work?just added this line of code if (!wp_is_post_revision($post->ID)) $pID = intval($post->ID); else $pID = wp_is_post_revision($post->ID);,but still nothing :(
Can someone help me with this,still previous post counter is increment,how to avoid that?