user147
user147

Reputation: 1310

Wordpress post visit count?

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.

Upvotes: 0

Views: 1817

Answers (1)

Benoit Courtine
Benoit Courtine

Reputation: 7064

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

Upvotes: 1

Related Questions