'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?

Sorry for my bad English.

Thanks.



Solution 1:[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'
));

Solution 2:[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

Solution 3:[3]

http://www.wpbeginner.com/wp-tutorials/how-to-track-popular-posts-by-views-in-wordpress-without-a-plugin/

Basically it's adding a meta field to each post - and deletes the old record when it's viewed, then replaces it with 'old record + 1'

Solution 4:[4]

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');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anuj Kumar
Solution 2
Solution 3 Xhynk
Solution 4