home
search
help
profile
liking the experience? our app is even better
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now  
Search
showing results for
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
upvote
downvote
source
similar questions
queries leading to this page