Skip to content

Instantly share code, notes, and snippets.

@matheuscl
Created March 24, 2015 18:34
Show Gist options
  • Save matheuscl/9eb23adb014402014db3 to your computer and use it in GitHub Desktop.
Save matheuscl/9eb23adb014402014db3 to your computer and use it in GitHub Desktop.
Function to set post views and set cache.
<?php
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count == ""){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '1');
wp_cache_set( "view_" . $postID, TRUE, "", 0 );
}else{
if( !empty( wp_cache_get( "view_" . $postID ) ) ){
$count++;
update_post_meta($postID, $count_key, $count);
wp_cache_set( "view_" . $postID, TRUE, "", 0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment