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.
A very basic solution would be to use the Transients API, an example…
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,
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,Where
192.160.0.5
is the IP and333
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 returnFALSE
.From the Codex;
…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,
…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 longerFALSE
, 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;Functions used in addition to OP,
Transients API -> read more
set_transient -> read more
get_transient -> read more
Roles and Capabilities -> read more
current_user_can -> read more
I came across this question while looking for something else, but I do have something to add.
The suggested solution is great, except for one minor flaw. Expired transients are only cleared from the options table when they are accessed. So with this solution, your options table will be getting longer and longer. Eventually with hundreds of megs stored in the options table from spammers exploring the blog creating records, your site will start to slow down.
One possible solution is to store a list of ip addresses in a single transient the post ID as the key. Then if the ip address isn’t in that list, you add it to the meta options. That still might get to be pretty big per post, but at least it won’t result in millions of records. If you write the date as the first line of the transient value, you can check to see if the date is today’s date and if it isn’t, start a fresh list with the current IP address.
There are probably other solutions involving storing the date in the key or storing an array in the function key valued to IP and date. There are lots of options. But you can’t let the transient list simply grow without consequences.