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.
Is it beyond the scope of this article to explain had you assign the functions to those buttons as in your screenshot:
Recent Articles – Popular Posts – editor’s picks
That is basically what I want to do…have buttons at the top of my posts page for recent, popular/trending, editors picks. I haven’t found any plugins that can do that. The all focus on side bar widgets.
It looks like your article is starting to talk about this but where to go from there is beyond my current knowledge level.
Thanks,
Chris
This was very helpful.!!! Thanks a ton.!
Getting double view count on refresh (single.php). Don’t know if the remove action is working or not for “adjacent_posts_rel_link_wp_head”. Useless so far. I tried everything that is possible to remove double count. Using this code in a custom made plugin.
Thsi thing works greats.
Now I want the columnn in admin to be sortable. any idea how to do this?
many thanks!
Hey, thanks for the code. Only issue I’m having is the view count is incrementing by 2 instead of 1 on refresh. Any thoughts?
I have the same problem with this code.
Have you found any solution of this?
This was happening for me because I had:
`set_post_views(get_the_ID());` in single.php
AND
`add_action( ‘wp_head’, ‘track_post_views’);` in my functions.php which was also adding `set_post_views(get_the_ID());` within it.
By removing the line on single.php I fixed the double count.
How to show most popular posts on my home page?
How do I do this for a weekly basis, most viewed posts each week?!
ah , this works fine ;
thanks dear !
Hello..
Using w3total cache but count not working correctly.
don’t use child theme. how can I fix it?
thanks..
Fixed this issue:
Cannot use WP_Query, used get_posts and it is now working. Then use a foreach look to loop through the posts and display them. See below:
$blog_cat_array = get_the_category();
$blog_cat = $blog_cat_array[0]->term_id;
$popularpost = array(
‘posts_per_page’ => 2,
‘meta_key’ => ‘wpb_post_views_count’,
‘orderby’ => ‘meta_value_num’,
‘order’ => ‘DESC’,
‘category’ => $blog_cat,
‘post_type’ => ‘post’,
);
$pop_posts = get_posts($popularpost);
foreach($pop_posts as $pop_post){
the_title();
}
Is there a possibility to count only unique visitors? Thank you
Simple Awesome …. (Y)
I really like this option and have built it into my site.
I have one question regarding the count. I found that the counts were rather high so I changed the code around a bit. With every count increase I wrote the IP address to a log file. I found hat two-third of the counts were legit and the other ones came from googlebot, apple, etc. and just now apews Is there any way to get the counts right and do you know if the ‘regular’ plugins have found a way around this?
Thx!
BTW: Keep up the good work, I thoroughly enjoy this site!
you can take maxmind ISP IP database and only count humans. This is the best option. Second just dont count common bots user-agent-names
Thanks so much. I will definitely look into that.
In the mean time I implemneted the solution which was implemented in the WordPress Popular Posts plugin. That works for now, but the solution you recommended seems more future-proof. Thanks again!
Hello, I am using WP Super Cache.
I do not know how this code works correctly.
It is not working for Custom Post Type. Can you help me on this please..
Hi! Awesome! This code helped me a lot!
Do you know how to display the posts with 0 views? I have to enter to the post page by using the url the first time, otherwise it doesn’t show.
Awesome thanks this is really useful, but a question. Doesn’t it slow down the loading of the page significantly?
Hi,
This is very useful post, i really appreciate. Can i filter the post in category, I wanted to show the post of specific category.
Thanks.
How can i set Features post on Mobilepress Hompage?
Please give me that code.
Thank you for this awesome post, you guys are the best
Ok this is great. For some reason, post count doesnt show numbers, not sure is it because i work on a local., but what i wanted to ask even more, is how to add so it count only in the last 7 days?
Hi, How can I show the most popular posts for the current week??
Awesome Article !! Thanks. Would be better if code was explained in detail.
I need advice on how r u to Load Word files to a WordPress website please. Is there an easy way?
Sadly this does not work with W3 Total Cache with Page Caching enabled. Coul not get any “fragmented caching” to work either.
Same here @DARKSAFKA . Not working.
nice code. work perfectly….
you are right
Hey,
This works great. But I need to show popular post of a day, this code shows popular posts of all time. Is there anyway to show popular posts of a day only.
Any help will be greatly appreciated.
Thanks
Try to add something like this in the WP Query
‘date_query’ => array(
array(
‘year’ => $today[‘year’],
‘month’ => $today[‘mon’],
‘day’ => $today[‘mday’],
),
This doesn’t seems to work. Is there anyone who figures out how to display the most popular posts of the lasts 7 days?
Thanks!
Add this code to the query
‘date_query’ => array(
array(
‘after’ => ‘1 week ago’
)
)
Hi, I use Goodnews 5.7.2 theme, but where i put “wpb_get_post_views(get_the_ID());” in my theme, thank for u’r guidance
Hey, thanks for the information.
There’s a little problem. I put:
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);
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’);
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’;
}
and my visits counts always as two. What’s happening? Thanks.
Really great instruction no need of plugin.works perfect.
What table does this custom field get stored into? Is it the posts table or the postmeta?
I modified this a little to use it as a shortcode. To use this with a shortcode, add this to your functions.php:
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);
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’);
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’;
}
function wpb_most_viewed_posts() {
// start output buffering
ob_start();
?>
4, ‘meta_key’ => ‘wpb_post_views_count’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘DESC’);
//begin loop
while ($query->have_posts()) : $query->the_post(); ?>
<?php
// Turn off output buffering
$theResult = ob_get_clean();
//Return output
return $theResult;
}
// Create shortcode
add_shortcode('wpb_most_viewed', 'wpb_most_viewed_posts');
//Enable shortcode execution in text widgets
add_filter('widget_text', 'do_shortcode');
Then simply add [wpb_most_viewed] to your desired page/post and it should display your most popular posts.
Hello. Huge thanks for that. Works very well in my theme.
Hello, great tutorial but I have one question.
After following all of the steps the template isn’t paginating. It’s only showing the default 10 posts. Should this happen or is there a way to get it to paginate?
Folks, please help. Just cannot figure it out.
How do I change the args to the wp-query to show the most popular posts for the last week? Or month?
Hello,
can I do this with comments? I dont use any comments on my site so I could use this comment count to check my most popular page without adding a comment?
Cheers,
Denis
How do i make this work with w3 total cache? I’ve tried the fragment cache suggestion but changes nothing
I’ve found a number of tuts covering this topic but none seem to spell out where the line goes.
I’ve tried it inside PHP tags and it breaks the page. If i place it in the HTML it just renders as a comment when you view source and no php is generated.
Any ideas? I’m w3 total cache and my page views aren’t getting updated
Thanks, this works as charm, but i didn’t get how to use it with the W3 Total Cache.
Hello! How can I show the most popular posts for the current week?? Is there any possible? Thanks in advance.
Hello,
I’m using this code for months and it’s works great! That’s before I start using W3 Total cache and the code stop counting view for me.
I struck at where and how do I need to put the mfunc code. Can you point that out for me?
FYI, I put all of the codes in site-specific plugin.
Hello,
I’ve using this code for months and it’s work greats! That’s until I start using W3 Total Cache and this code stop count views for me.
I’m struck at where do I need to put the mfunc to let the code work with cache. Can you point that out?
FYI, I put all the code in a site-specific plugin.
Hell this is great! How would I display the view count outside of the post loop like in the sidebar?
hello, I have some problem on how setting up like when the login user won’t include on the count while viewing any pages?? how to do that.. please need some help on these. thanks
Hey there, thanks for this. REALLY helpful!! Would you know how to apply a time range to this code? For example to show the most popular posts in the last day, week or month etc? I know there are plugins for this but I would like to do it without one
Great!
Hello,
thanks for this nice tutorial. It works on my page!
– how can I exclude robots and spiders that hit my posts?
– May be I can set a timer of 10 seconds. after that the count should rise. So the people who only click thourgh the posts are not counted.
Cheers,
Denis
You have no idea how much time you saved me. Thank you, works perfectly!
Thanks for the tip. Will use.
Hello – thanks so much for this! I noticed the question regarding the W3TC workaround, but have a slightly different question: does that still apply if I’m hooking into wp_head from functions.php, and if so, how exactly do I implement it there? Thank you!
this is really easy and very helpful! thanks man!