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've been looking for function about post views count. I found this simple functions using update and get post meta.

function setPostViews($postID) {
    $count_key = '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++;
        update_post_meta($postID, $count_key, $count);
    }
}

What I want is, to add functions to limit the count added per IP by given time - a day for example. So when the IP visiting on the same day, it would not counted, just once.

Any idea? Thanks.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

A very basic solution would be to use the Transients API, an example...

function setPostViews($postID) {

    $user_ip = $_SERVER['REMOTE_ADDR']; //retrieve the current IP address of the visitor
    $key = $user_ip . 'x' . $postID; //combine post ID & IP to form unique key
    $value = array($user_ip, $postID); // store post ID & IP as separate values (see note)
    $visited = get_transient($key); //get transient and store in variable

    //check to see if the Post ID/IP ($key) address is currently stored as a transient
    if ( false === ( $visited ) ) {

        //store the unique key, Post ID & IP address for 12 hours if it does not exist
        set_transient( $key, $value, 60*60*12 );

        // now run post views function
        $count_key = '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++;
            update_post_meta($postID, $count_key, $count);
        }


    }

}

The example above works, although it can be expanded to exclude administrators, editors or any other logged in user class/role you would like. This is just to get you going!


Step by step for those that want know what's happening, where.

Using the Transients API we can store values for a predefined period of time, in this example we are storing the result for 12 hours, for 24 hours you would do,

set_transient( $key, $value, 60*60*24 );

What happens when the visitor lands on the post is that we store their IP address in the variable $user_ip.

Then we create a unique key value for our transient which combines the post ID and user IP (i.e. $user_ip . $postID) together, an example would look like,

192.160.0.5x333

Where 192.160.0.5 is the IP and 333 is the Post ID (x is just an example separator to help distinguish the two segments should you need to use those values for any other purpose such as debugging etc)

We then create a $value variable which is an array of any values you wish to store for the transient. In this case we store the IP and Post ID as separate values. You don't need to store them as an array, you could concatenate them together into one string if you would like but by storing them in an array it allows you to more easily iterate over the results of the transient and return the values separate for any purpose you would like, such as logging the results. (modify to suit)

Now we get the transient using get_transient($key) and store it in a variable named $visited. If the transient does not exist or is expired it will return FALSE.

From the Codex;

If the transient does not exist, or has expired, then get_transient will return false. This should be checked using the identity operator instead of the normal equality operator, because an integer value of zero (or other "empty" data) could be the data you're wanting to store. Because of this "false" value, transients should not be used to hold plain boolean values. Put them into an array or convert them to integers instead.

...and that's why we check the existence of our transient value using the identify operator === like so,

if ( false === ( $visited ) ) ...

Now at this point if $visited (our transient) does not exist (evaluates to FALSE) we run our conditional statement and its contents.

First we set the transient value using the variable data we set up outside of our conditional statement,

set_transient( $key, $value, 60*60*12 );

...which adds the unique key and values to the database for 12 hours.

Next we actually run the function which will store a unique post view for the given post that's being served.

Lastly, if and when the user returns to this post within a given 12 hour period our setPostViews function will fire off but this time, since $visited now holds a $key value and is no longer FALSE, the conditional statement will not run and store additional post views.

UPDATE

If you want to exclude views from a certain user role, like the admin, wrap the function within a current_user_can conditional statement like;

function setPostViews($postID) {

    //check if user not administrator, if so execute code block within
    if( !current_user_can('administrator') ) {

       //all code goes here...

    }

}

Functions used in addition to OP,

share|improve this answer
1  
Wow, such great! many thanks, dude :D –  rizqyhi Sep 15 '12 at 16:54
    
No problem, enjoy. Small update to allow exclusion of admin account from stats. –  userabuser Sep 15 '12 at 17:39
    
yeah, better now :D I've read the ways transients work but didn't how to implement it. –  rizqyhi Sep 15 '12 at 22:23
1  
Brilliant. Will include this in my theme I'm working on. +15 for this. –  Pieter Goosen Feb 25 at 9:50

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.