1function setPostViews($postID) {
2
3 $user_ip = $_SERVER['REMOTE_ADDR']; //retrieve the current IP address of the visitor
4 $key = $user_ip . 'x' . $postID; //combine post ID & IP to form unique key
5 $value = array($user_ip, $postID); // store post ID & IP as separate values (see note)
6 $visited = get_transient($key); //get transient and store in variable
7
8 //check to see if the Post ID/IP ($key) address is currently stored as a transient
9 if ( false === ( $visited ) ) {
10
11 //store the unique key, Post ID & IP address for 12 hours if it does not exist
12 set_transient( $key, $value, 60*60*12 );
13
14 // now run post views function
15 $count_key = 'views';
16 $count = get_post_meta($postID, $count_key, true);
17 if($count==''){
18 $count = 0;
19 delete_post_meta($postID, $count_key);
20 add_post_meta($postID, $count_key, '0');
21 }else{
22 $count++;
23 update_post_meta($postID, $count_key, $count);
24 }
25
26
27 }
28
29}
30