Here is the simple wordpress code, for track your individual post, pages without using a plugin.
Add this snippet into the themes functions.php of your wordpress theme then follow step 1:
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);
}
}
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
Place this snippet bellow "setPostViews" within the single.php inside the loop.
<?php
setPostViews(get_the_ID());
?>
Place the following code within the template where you would like to display the number of views.
<?php
echo getPostViews(get_the_ID());
?>
Responses
0 Respones to "How to Track post views without a plugin using post meta"
Post a Comment