WordPress – get 5 popular posts by views without plugin

Hi,

I have custom fields with image from posts, and I want to display the top 5 posts sorting by views. I am using WordPress, can you help me please?

Read More

Sorry for my bad English.

Thanks.

Related posts

Leave a Reply

4 comments

  1. There’s one error with Xhynk’s reference:

    The query it runs returns posts in alphabetical order (1, 2, 20, 23, 3, 4, etc)

    You just need to change

    'orderby' => 'wpb_post_views_count'
    

    to

    'orderby' => 'meta_value_num'
    

    For the top 5, use:

    $popularpost  = new WP_Query(array(
        'posts_per_page' => 5,
        'meta_key' => 'wpb_post_views_count',
        'orderby' => 'meta_value_num',
        'order' => 'DESC'
    ));
    
  2. It is very easy. Just use this code into your functions.php

    /*
     * Set post views count using post meta
     */
    function setPostViews($postID) {
        $countKey = 'post_views_count';
        $count = get_post_meta($postID, $countKey, true);
        if($count==''){
            $count = 0;
            delete_post_meta($postID, $countKey);
            add_post_meta($postID, $countKey, '0');
        }else{
            $count++;
            update_post_meta($postID, $countKey, $count);
        }
    }
    

    put single.php

    setPostViews(get_the_ID());
    

    This is your popular post query:

       <?php
          query_posts('meta_key=post_views_count&posts_per_page=5&orderby=meta_value_num&
          order=DESC');
          if (have_posts()) : while (have_posts()) : the_post();
       ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title();
         ?></a>
       </li>
       <?php
       endwhile; endif;
       wp_reset_query();
       ?>
    

    For details go

  3. Kabir Hossain’s solution can be shortened:

    Since get_post_meta() returns false if no meta has been found, we can simply use this code to record views:

    /*
     * Set post views count using post meta
     */
    function setPostViews($postID) {
        $countKey = 'post_views_count';
        $count = get_post_meta($postID, $countKey, true);
        update_post_meta($postID, $countKey, ((int)$count)+1);
    }
    

    ((int)$count) means that if $count is false it will become 0;

    Then, there’s no real need to delete post meta as there’s no way to decrease the number of views with our code. The function will run on the first post load and create the meta. If the meta has been created before this function has been run, then, well, we’ve had stats on this post that we can update.

    Also, there’s no need for add_post_meta (especially adding 0 on the first view), as the update_post_meta() will create meta if it does not exist.

    I also added a check to exclude admins from view stats and to allow this to run exlusively on posts, and hooked the function to template redirect:

     //place your theme's functions.php
     /*
     * Set post views count using post meta
     */
     function setPostViews($postID=null) {
         if(!is_single() || 'post' !== get_post_type() || current_user_can('administrator')) return;
         $postID = !empty($postID) ? $postID : get_the_ID();
         $countKey = 'post_views_count';
         $count = get_post_meta($postID, $countKey, true);
         update_post_meta($postID, $countKey, ((int)$count)+1);
    }
    add_action('template_redirect', 'setPostViews');