Skip to content

Instantly share code, notes, and snippets.

@MrJoshFisher
Last active December 13, 2020 00:05
Show Gist options
  • Save MrJoshFisher/ba982e3ea177ff35a10f304e48f007bd to your computer and use it in GitHub Desktop.
Save MrJoshFisher/ba982e3ea177ff35a10f304e48f007bd to your computer and use it in GitHub Desktop.
[Track WordPress Post Views by Using Post Meta] #WordPress
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
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, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment