In the past we have shown you how to create a popular post tabber in WordPress using a plugin. That plugin works great out the box for tabbers. However, we wanted more customization in our layout, so we decided to do it without a plugin. In this article, we will show you how to track and display popular posts by views in WordPress without using any plugins.
An example of our custom popular post display is shown in the screenshot below:

First thing we need to do is create a function that will detect post views count and store it as a custom field for each post. To do this, paste the following codes in your theme’s functions.php file or better in a site-specific plugin:
function wpb_set_post_views($postID) {
$count_key = 'wpb_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);
}
}
//To keep the count accurate, lets get rid of prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
Now that you have this function in place, we need to call this function on the single post pages. This way the function knows exactly which post gets the credit for the views. To do this, you would need to paste the following code inside your single post loop:
wpb_set_post_views(get_the_ID());
If you are using a child theme or you just want to make things easy for yourself, then you should simply add the tracker in your header by using wp_head hook. So paste the following code in your theme’s functions.php file or the site-specific plugin:
function wpb_track_post_views ($post_id) {
if ( !is_single() ) return;
if ( empty ( $post_id) ) {
global $post;
$post_id = $post->ID;
}
wpb_set_post_views($post_id);
}
add_action( 'wp_head', 'wpb_track_post_views');
Once you have placed this, every time a user visits the post, the custom field will be updated.
Note: If you are using a caching plugin, this technique will NOT work by default. We are using W3 Total Cache, and it has the feature called Fragmented Caching. You can use that to make this work just fine. Here is what needs to be changed:
<!-- mfunc wpb_set_post_views($post_id); --><!-- /mfunc -->
Now, you can do all sort of cool stuff such as display post view count, or sort posts by view count. Lets take a look at how to do some of these cool things.
If you want to display the post view count on your single post pages (often next to the comment count or something). Then the first thing you need to do is add the following in your theme’s functions.php file or the site-specific plugin.
function wpb_get_post_views($postID){
$count_key = 'wpb_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';
}
Then inside your post loop add the following code:
wpb_get_post_views(get_the_ID());
If you want to sort the posts by view count, then you can do so easily by using the the wp_query post_meta parameter. The most basic example loop query would look like this:
<?php $popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) ); while ( $popularpost->have_posts() ) : $popularpost->the_post(); the_title(); endwhile; ?>
To add other WP_Query parameters such as time range, refer to the WP_Query page on Codex.
We hope that you enjoyed this post.








I’m still learning this stuff so pardon my ignorance.
How do you allow the user to choose between queries like they do on codecanyon when they allow you to sort by price, sales, date etc.?
Thanks so much.
How can I change number of popular posts that display?
Change the posts_per_page to whatever number you like.
Thank you so much for this. Life saver and a great tip that I definitely will be using more often.
Hey, Really , Many Many thanks for this useful tips. I am highly glad to you.
Hi , that’s great !
But ‘orderby’ => ‘wpb_post_views_count meta_value_num’ not working.
please use : ‘orderby’ => ‘meta_value_num’
thnx
Thanks, it works for me, just with one important exception – popular posts are not as links, just their titles. How can I fix this, please?
I added this code in the manner described in the article and upon activation, I saw this….
The plugin generated 2 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
Fixed. I did two things:
1. Switched my permalink structure to a custom structure /%category%/%postname%/
2. Check through all of my pages for extra spaces.
One of them worked.
…go figure
Preston
Hi, I think it´s necesary add – wp_reset_query(); – at the end of the query to destroys the previous query used on a custom Loop.
I hope It helps somebody.
I have this working to 95% using a custom WP Query to display popular posts from each category.
The only bit not working for me is the order – mine won’t display in descending order of views.
Thanks for the post! This really helped.
I’m not sure if anyone else ran into this issue, but when you set up the arguments for WP_Query, you have orderby => ‘wpb_post_views_count’. This was a problem for me because I wasn’t sure how it was ordering my posts. In the codex it says that if your using numbers they will only sort by the first digit. To fix this, you can simply replace the ‘wpb_post_views_count’ with ‘meta_value_num’. This basically will tell the query to reach inside the post’s meta value and probably cast it to an integer before it sorts. Hope this helps anyone running into the same issues.
Overall, it works great! I have the 4 most popular posts in a slider on the home page. Thanks again!
Good suggestion. Updated the article with this.
Hi. Your code sample up top is *not * updated.
Instead of this:
‘orderby’ => ‘wpb_post_views_count meta_value_num’
you should have this as suggested :
‘orderby’ => ‘meta_value_num’
if you want to sort by “Most to Least”
Why it only shows posts which have count view < 100?
If you are using a caching plugin, then it doesn’t always update.
Thanks for the tutorial. How do you exclude current posts from displaying?
This looks great on my home page but it seem to want to display on my single.php or anywhere else on my site. I tried creating a sidebar-single.php and inserting the code but still no luck. Any idea why it wouldn’t work on other areas my theme?
i got it working. Thanks for this.
Dont work, its show randomic posts :S and i use post_type = > ‘post-type-name’
This usually happens when meta key wpb_post_views_count is not available for posts, make sure you add the function that tracks views within wp while loop, otherwise it will keep showing random posts.
- Mody
Hello,
I’m using your code for track post view in the wordpress theme.
function wpb_get_post_views($postID){
$count_key = ‘wpb_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’;
}
The problem is that when I use W3 Total Cache the track view is not working right.
Is there a way in the W3 Total Cache’s Options to put ignore only on this function, but in the same time I want the code to work with W3 Total Cache?
Thank you!
Read the article again. We have already covered this “Fragmented Caching”.
Hello
i followed your tutorials and have done exactly what u said.
i also added ur snippet
php query_posts(‘meta_key=post_views_count&orderby=meta_value_num&order=DESC’);
in index.php
i am facing a little error,
i am using infinite scrolling
when i put this snippet in index.php
the infinite scroll instead of loading next set of post
loads the same sets of post
For better Understanding u can check it live here
blog.newgags,com
For some reason my post views are incrementing by 2 on each page refresh. What could be happening there?
I do apologise. I had added the tracker to both the WP header and also to the single post body. Very stupid on my part :}
Nice tutorial, very easy to follow.
First of all thanks for this post. Second I have been using this script for a few days now and for some reason it started out fine and now it is not displaying the most viewed posts, I don’t see any rhyme or reason to what posts are now being displayed. The last I checked the post that it is displaying at the top has 8 page views. I know there are posts with 25+ recorded page views. I would like to figure this out. Can you possibly point me in the right direction. I followed this post word for word. The post views are being recorded properly.
Not sure what could be going wrong. The WP_Query is suppose to list the posts with the highest counts first.
Hi. This works grade up until the view count gets over 999. All post with more view counts than 999 are not displayed, the query never post them. The latest post is the one with exactly 999 and the rest are under that. I have over 100 post that have more than thousand and are not being included.
Interesting. We have posts with over 10k views and it seems to be working just fine.
Guys you rockkk
Quick question:
If i have add a custom post type in your code ?
(popular post from especific custom post type)
Regards (:
This should work with CPTs as well.
This is a good basic tutorial, but be warned: it’s not going to work if you use caching strategies that bypass PHP (like wp-supercache, W3TC, nginx/varnish, etc). The only way to count those would be via Javascript or log parsing.
Hey Artem, Thanks for dropping by. Actually using W3 Total Cache, you can use fragment caching and it works just fine. Going to update the article for those who are using the caching plugin.
Interesting. However, I run nginx in front of W3TC, and it does a whole lot of its own caching, so it’s always safer/more reliable to use an AJAX approach. Nice info on the fragment caching though, I had no idea W3TC had it.
Why use
//To keep the count accurate, lets get rid of prefetching
remove_action( ‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0); ?
if you can use the main loop or the footer ?
Some browsers prefetch the rel links with the next value. So technically when a user views one post, it can act as if they had viewed both posts. This will cause inaccurate count. If you like inflated views, then don’t take it off.
Great article! Explanation useful and easy to understand.
Thank you.
Yay! A tutorial that doesn’t require a plugin!
Yeah we try to balance things out for our audience. This was requested by the users, and we were using it on our own site.
Please stop saying stuff like this. Plugins aren’t bad, it’s articles like this that make them look bad. Enough articles have come out over the past few weeks to fully explain this – it’s getting a little embarrassing.
How would you suggest titling future posts instead? DIY prefix? These are different then just using a pre-made plugin. Also, if you read the article, there is no where in our article that we say “plugins are bad”. We clearly state that the only reason why coded this was to get more customization. It is up to a user to take either stance. Some can think that plugins are bad… whereas others like yourself can think that we are saying that plugins are bad…
I don’t think any type of prefix is needed. It’s one of the great/scary things about WordPress. You COULD put this code in your theme, but then begs the argument about needing to either, 1) Loose those customizations when you change your theme, or 2) have the knowledge to properly pull those over to another theme.
A lot of the users here are beginners (hence the point of this site), so many will just copy/paste what you give them. WP Beginner is obviously a fantastic resource (reason why I follow you on Twitter), but you have a responsibility to not put a false notion about how plugins/themes work.
Correct, you don’t flat out say, “Plugins are bad, put this in your theme instead!”, but the original commenter to the thread I replied to, said, “Yay! A tutorial that doesn’t require a plugin!” – so even though you didn’t say it, that’s how it was taken.
I’d suggest doing was Pippin does for his plugins – he has a simple starter plugin he uses for all of his tutorials. Why not create a blank “Starter Plugin” download, with just the basics, so others can download and put their customizations in there instead? Reference it in each article you do and it takes out some of the confusion. Thanks.
We have been following Otto’s advice on site-specific plugin for quite some time. It is probably similar to what Pippin does. If you read this article, site-specific plugin is hyperlinked. It is in most other articles as well. That article shows users the importance of site-specific plugin and advise users to not put everything in functions.php file. At the bottom of that article, the sample “starter plugin” is there for anyone to start with.
As Zach said, please stop using “without a plugin”. Sure, it’s cool to see how to code this yourself but there is literally no difference between this code and the code in a plugin. You could place this code into a plugin and it would function identically to placing it in your theme.
i am using genesis child theme..can u tell me which all functions and codes to use..i am sorry i am a noob to genesis…
For child theme users, the wp_head solution would work to track post views. Not sure what else you meant.
is it true? as artim told, does this code become unresponsive when we use a caching plugin?? for example i currently use W3 total cache and i want to use this method to build my custom popular post by views widget…i read this kind of post on wpsnipp.com and the users suggested it does not work when we enable caching plugins…reply soon…
Read the comment reply to Artem. You can use fragment caching to make it work just fine.