Skip to content

Instantly share code, notes, and snippets.

@jshrssll
Last active November 26, 2019 23:46
Show Gist options
  • Save jshrssll/8d03d9c14ad37b0240995db4fa3803a7 to your computer and use it in GitHub Desktop.
Save jshrssll/8d03d9c14ad37b0240995db4fa3803a7 to your computer and use it in GitHub Desktop.
Increment page view count in Wordpress
function theme_set_post_views($id) {
$count_key = 'theme_post_views_count';
$count = get_post_meta($id, $count_key, true);
if ($count == '') {
$count = 0;
delete_post_meta($id, $count_key);
add_post_meta($id, $count_key, '0');
} else {
$count++;
update_post_meta($id, $count_key, $count);
}
}
// Usage:
// theme_set_post_views(get_the_ID());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment