Created
March 24, 2015 18:34
-
-
Save matheuscl/9eb23adb014402014db3 to your computer and use it in GitHub Desktop.
Function to set post views and set cache.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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