Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Add this to functions.php
- 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);
- }
- }
- // Place this snippet bellow โsetPostViewsโ within the single.php inside the loop
- <?php setPostViews(get_the_ID()); ?>
- // Add this to your template
- <?php
- query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC');
- ?>
- Source:
- http://wpsnipp.com/index.php/functions-php/track-post-views-without-a-plugin-using-post-meta/
- http://wpsnipp.com/index.php/loop/most-popular-posts-using-views-post-meta/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement