Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Display Popular Posts by Views in WordPress without a Plugin

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Display Popular Posts by Views in WordPress without a Plugin

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:

Popular Posts Example

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.


Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Philipp says:

    Is there a possibility to count only unique visitors? Thank you

  2. Zeeshan says:

    Simple Awesome …. (Y)

  3. Erwin Barendregt says:

    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!

    • bah says:

      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

      • Erwin Barendregt says:

        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!

  4. HiepTD says:

    Hello, I am using WP Super Cache.
    I do not know how this code works correctly.

  5. Krishna says:

    It is not working for Custom Post Type. Can you help me on this please..

  6. John says:

    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.

  7. Gianmarco says:

    Awesome thanks this is really useful, but a question. Doesn’t it slow down the loading of the page significantly?

  8. ayaz says:

    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.

  9. Md Maruf Adnan Sami says:

    How can i set Features post on Mobilepress Hompage?
    Please give me that code.

  10. gift charles says:

    Thank you for this awesome post, you guys are the best

  11. Bojan says:

    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?

  12. Blown says:

    Hi, How can I show the most popular posts for the current week??

  13. Ashish says:

    Awesome Article !! Thanks. Would be better if code was explained in detail.

  14. Dhiraj Kataria says:

    I need advice on how r u to Load Word files to a WordPress website please. Is there an easy way?

  15. DarkSafka says:

    Sadly this does not work with W3 Total Cache with Page Caching enabled. Coul not get any “fragmented caching” to work either.

  16. Riya says:

    nice code. work perfectly….

  17. REIBI says:

    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 :)

    • Dimitrios Arkolakis says:

      Try to add something like this in the WP Query

      ‘date_query’ => array(
      array(
      ‘year’ => $today[‘year’],
      ‘month’ => $today[‘mon’],
      ‘day’ => $today[‘mday’],
      ),

      • Nick Heurter says:

        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!

  18. Mawardiy says:

    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

  19. Alex says:

    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.

  20. 6b says:

    Really great instruction no need of plugin.works perfect.

  21. Paritosh Arya says:

    What table does this custom field get stored into? Is it the posts table or the postmeta?

    • ScoDal says:

      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.

  22. Marcos says:

    Hello. Huge thanks for that. Works very well in my theme.

  23. John says:

    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?

  24. Varange says:

    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?

  25. Denis says:

    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

  26. Kes says:

    How do i make this work with w3 total cache? I’ve tried the fragment cache suggestion but changes nothing

  27. Kes says:

    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

  28. jarc100 says:

    Thanks, this works as charm, but i didn’t get how to use it with the W3 Total Cache. :P

  29. Jorge says:

    Hello! How can I show the most popular posts for the current week?? Is there any possible? Thanks in advance.

  30. Bigdragon13th says:

    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.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.