Last active
November 26, 2019 23:46
-
-
Save jshrssll/8d03d9c14ad37b0240995db4fa3803a7 to your computer and use it in GitHub Desktop.
Increment page view count in Wordpress
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
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