Track the number of times a WordPress post has been viewed
This is a neat little snippet to track the number of times a WordPress post has been viewed, used wisely it can be used to show a trending post or popular content. In the backend you’ll discover gold gems of content and it can be used to display trending content in the frontend, it could be a blog post, a product, a review or just a popular page that gets a lot of attention! Very useful for your visitors. So, there are a couple of elements to this snippet; a couple of functions and a means to register the data in a post, lets begin…
For the function:
//Track post views without a plugin using post meta 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); } }
The function above will create a custom field called ‘post_views_count’ attached to the ID of a post that will record the data but, if we have no data a little bit of organic funky creation will happen to create it as zero (0), if it does exist (even if it is 0) we add 1 more view to the data and save it and this is how we clock the views. Sweet eh!
To take advantage of the function we need to register the view in a template or custom post type, this is done by adding the function within the WordPress standard loop so the ID of the post is registered:
setPostViews(get_the_ID());
To query the content based on views:
$get_trending = array( 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'posts_per_page' => 5 ); $trendy = new WP_Query($get_trending); while ($trendy->have_posts()) : $trendy->the_post(); echo '<ul><li><a href="'; echo the_permalink(); echo '">'; echo the_title(); echo '</a></li></ul>'; endwhile; // Reset Post Data wp_reset_postdata();
hopefully this gives you the gist of this awesome function resource
This is good. It’s worth noting that this will give very different results to a proper analytics tool as it will track any generation of the page, including admin views and crawls from search engines and other robots/crawlers.
It’s easy to exclude some of these things if you want to.
Good point Ross, admins will generate numbers but, I’m not sure I have seen results from crawlers though to be honest. I’ve been running with this snippet on our own site for a while and I know that new posts do get indexed by Google but, the posts do not get a page view added from the crawlers. Of course I could be wrong it has been known before :-/
I guess it’s just a note to be careful how you use it. I used this technique on one site and got twice as many hits as was showing on Google Analytics which confused my client (and me, for a while).
The other thing to watch is that if you’re using a caching plugin you may fail to record hits because you don’t always run the loop/template.