Be Studios

Intercontinental Design Firm

This is our knowledge center. Enjoy!

WordPress: Display Most Popular (Viewed) Posts without a plugin

OK this one was tricky, but resolved. With this method you can display most popular posts by views (not by comments, in my casi I hadn't comment on each post). It uses a custom-value that records each hit on each post and then it is possible to query them by meta-value: Add this on functions.php:

function getPostViews($postID){
    $count_key = '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 setPostViews($postID) {
    $count_key = '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);
    }
}

The add this when the loop starts in single.php:

setPostViews(get_the_ID());

Finally, query the posts by this meta-value, for example in the sidebar.php:

<?php
    query_posts('meta_key=post_views_count&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();
?>

Comments

Tik: Thanks!

Jmurimi: Your post was of great help

Hasibul Islam: Thanks so much. Its working very nice in local host, but when I upload online server, not working................... Can u help me????

upye: thanks it's so helpful for make my blog look cool with you popular view

Carol: Is there a way to make this work for custom post types as well as posts? And to limit it to the top X number of posts?

saeed: How can I change the number of posts that display?

jorge: To limit the number of posts, just adapt your query using query_posts parameters: posts_per_page=5 (to limit to 5 posts), so: query_posts('meta_key=post_views_count&orderby;=meta_value_numℴ=DESC&posts;_per_page=5');

Maicon Voltolini: Muito Ăștil! Muito Obrigado! Thanks! ;.html)

Kingsley: I use genesis theme, and there is no such thing as single.php any idea where to put the second code??

herrfischer: finally one working solution, great!

Fernon: Hi is there a way to make a plugin out of this? I have an assignment where I have to customize the code and make a plugin out of it. Hope someone could help me out! Thanks

Shahmy: Thanks a lot..

matze: Thanks, works great. How can i display the number of views in the sidebar?

misdreavus79: To include custom post types, you have to add the "setPostViews(get_the_ID());" snippet in your custom post home page. For example, if you have a flower custom post type, you want to add the snippet on the single-flower.php loop. As per displaying the number of views in the sidebar, you can use "get_post_meta($post->ID, 'post_views_count', true)" and it should display the count.

ivan: Hello, I am wanting to switch from using the plugin because it doesn't support the polylang language (the author's not getting back to me and I don't really know if it's possible to filter functions for the plugin..) Two questions: - I examined the code a bit and I'm not sure if this code starts counting posts from when it's implemented? Or does it somehow retrieve where the post counts are currently? - The current code on this website uses the get_posts() function so it creates an args array instead of using WP_Query(). Is this the same thing? I'm guessing not. And if it isn't is the code below correct to get the array? I tried implementing this but it didn't seem to work. $args = array( 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC','numberposts' => 6, 'post_status'=>"publish",'post_type'=>"post",'lang' => 'en'); thanks!

zulsdesign: how to display popular post with this code by month or 3 month? thanks

jorge: zulsdesign: you can use any argument for get_posts() function, for example, to get popular posts for current month: query_posts('monthnum=' . date( 'n', current_time( 'timestamp' ) ) '); More info: WordPress Codex.

jorge: ivan: your code looks fine, try to remove args to check if any of them is breaking your query.

ivan: Thanks Jorge I actually realized that the functions didn't seem to lead up. So I put them up. I'm confused about a few things, one is, with the posts, I also echo the post number by calling: echo 'post_views_count =', $post_views_count; And there is no value that gets displayed. Do you know how come? Also, posts are being displayed and I don't know if they are the most popular ones either. Especially since I purposely outputted them using this code here: http://hushmagazine.ca/es/ And this one is by using the already existing plugin, and the posts are totally different: http://hushmagazine.ca/ Thoughts? Also, in response to your suggestion, what did you actually mean by "removing args"? Thanks again!

eliza: i've tried it but nothing happened

Iswandi: Thank you, it works including howmany most-viewed posts I want to display

Nikhil MIshra: Thanks buddy just got what i want from last few days

Jamonzito: Not working, put the code on function.php , addd the thing to the loop And I call the thing on my sidebar 7,849 views | 49 comments for some reason its not working.... at all not displaying anything

Md Abul Bashar: Thanks for share, very nice tune, it is working.

fakhri: how can I customised the look in css? I want to show it with numbering. Could you please help me with the css? Thank you

Harsh: Thanks for the code, how can I make it specific to current category or tags view?