May
19
2009

How to List Your Most Popular Posts in WordPress

by   |  Posted in Tutorials  |  36 comments

I decided my sidebar was looking a little plain. It needed something more than just ads, so I placed a list of my popular posts there. I think it looks pretty cool and it was very simple to do. I just created a PHP function and added it to my functions.php files and then added some styles to get it to look the way I wanted.

First lets create the function. We’ll call it popularPosts. Be sure to add it between tags.

function popularPosts($num) {
    global $wpdb;
 
    $posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");
 
    foreach ($posts as $post) {
        setup_postdata($post);
        $id = $post->ID;
        $title = $post->post_title;
        $count = $post->comment_count;
 
        if ($count != 0) {
            $popular .= '<li>';
            $popular .= '<a href="' . get_permalink($id) . '" title="' . $title . '">' . $title . '</a> ';
            $popular .= '</li>';
        }
    }
    return $popular;
}

Now to call the function, all we need to do is place this code wherever we want our list to appear. Let’s call for a list of 10 items by placing a ’10′ as our $num variable when we call our function.

<div class="popular">
    <h2>Most Popular Posts</h2>
    <ul>
        <?php echo popularPosts(10); ?>
    </ul>
</div>

All that is left is some styles to make it look good. Here are the styles I used for my sidebar list.

.popular {
	clear: both;
	float: left;
	margin: 10px 0;
	width: 283px;
	}
 
	.popular ul, .popular ul li {
		margin: 0;
		padding: 0;
		font-size: 12px;
		}
 
		.popular ul li {
			margin: 4px 0;
			padding-left: 20px;
			background: url(images/star.png) no-repeat 2px 2px;
			}
 
	.popular h2 {
		border: 0;
		border-bottom: 1px solid #aaa;
		font-size: 22px;
		font-weight: normal;
		font-family: Georgia,serif;
		margin: 0 0 15px;
		padding: 0 0 5px;
		}

For the star, I used this image star which I created in Illustartor.

Just right click above or drag the image to your desktop and add it to the appropriate directory to complete the look. You can also replace that image with whatever you want to appear there.

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
Share the love...

Tags: , , , , , , , , , ,

Short URL: http://bit.ly/bsEieb

Discussion 36 Comments

  1. Christopher Ross on May 21, 2009 at 9:19 am

    Chris, this is a fantastic tutorial! You might want to add a couple quick things to your SELECT statement before the ORDER BY, “WHERE post_type=’post’ AND post_status = ‘publish’” these two limiters will ensure that the popular post is in fact a post (instead of a page) and that it’s published.

    Fantastic tutorial! Thanks for posting these.

    • Raymond Selda on June 27, 2009 at 2:03 pm

      Nice tip on adding a couple of filters on the query. Thanks Christopher.

  2. Zack Katz on June 4, 2009 at 12:13 pm

    I prefer the popularity contest plugin & widget, since it takes into account many more variables than just comment count, such as page views. Comment count is a good start, but not the whole ranking equation.

  3. Raymond Selda on June 27, 2009 at 2:01 pm

    Very helpful tutorial. Thanks a lot for this one. Already implemented this on my homepage.

  4. Adi on July 9, 2009 at 10:21 pm

    Hi
    This tips, using comment to determine the most popular post..
    How if I want using the views to determine popularity?
    And, do you know how to show the image of the post rather than the Text Link?

    Thanks

    • c.bavota on July 11, 2009 at 3:57 pm

      I am trying to figure out how to use hits as well. As soon as I do you I will post it.

    • abiazka on February 22, 2010 at 2:47 pm

      still waiting for this tutorial ..

  5. Lloyd on July 13, 2009 at 1:12 pm

    Thanx for this, already added it to my new project.

    Keep up the good work.

  6. jayesh patel on August 10, 2009 at 8:15 am

    that’s a good posting because we don’t need any plugin for showing popular post.

    • jayesh patel on August 10, 2009 at 8:22 am

      good posting..

  7. oes tsetnoc on September 18, 2009 at 8:20 am

    Hmmm. i want to try this one again in my site. Why is it that everything that i read in here is that i just wanted to implement them. Guess i really wanna learn huh?

  8. Zack on November 4, 2009 at 8:11 pm

    Nice codes, clear and simple, but will unproved comments being count? What abut password protected posts? I found another codes, please let me know if there is any bugs I missed.

    http://zacklive.com/wordpress-getting-most-popularcommented-posts/589/

  9. Mami on February 22, 2010 at 11:14 am

    Wow, this one is pretty clear and simple! I used some Plugins but they do not give me the really most popular Topics out.

  10. flexlearner on February 27, 2010 at 10:13 am

    good post,but its too complex to include her is a code better than others http://flexlearner.com/2010/02/25/wordpress-popular-post-code/ just copy paste where ever you need and see the magic,any way keep up the good work.

    • c.bavota on February 28, 2010 at 10:51 am

      That is a fine piece of code but inline styles are not the greatest approach. Of course, copying and pasting only one block of code is always much easier but not having control over what you are pasting in kind of defeats the purpose.

  11. RodgerFox on March 25, 2010 at 6:49 am

    good post, ths

  12. Jonathan on April 6, 2010 at 3:16 pm

    This is great, just what I have been looking for. I always hate doing things with plugins that can be so easily accomplished right in the theme.

  13. guadagnare on April 19, 2010 at 11:03 am

    Clear and simple code.
    But I still prefer plugins. One of the best WordPress features is indeed plugin support. What if you change theme?

  14. best products reviews on May 16, 2010 at 9:04 pm

    Great job there! Took 2 minutes to implement… Gotta love wordpress tweaks! I didn’t want to use a plugin because I already have enough as it is… very simple tweak and very effective!

  15. Sumeet Chawla on June 6, 2010 at 10:37 am

    Awesome tutorial.
    I got some doubts which I am facing. What ever I wanted to do, is working fine at my localhost, but when I upload it on my host then $wpdb->get_results is returning null. Am I missing something?

  16. Mike on June 21, 2010 at 12:55 pm

    Very nice chunk of code! I’ll use it! :)

  17. Jamie on June 24, 2010 at 5:00 am

    WELL happy with this bit of code. Really easy to implement and gives me complete control. Thank you!

  18. Wordpress SQL Queries on September 9, 2010 at 2:13 am

    this tutorial and query is really very nice and it can be easily used by developers, but i made some little modifications in it, that might be very helpful to get the exact data.
    here is the sql query that will produce better output once run in phpmyadmin
    SELECT comment_count, ID, post_title FROM wp_posts where `post_password`=” ORDER BY comment_count DESC LIMIT 0 , 10

    it will include only those posts which are not password protected.
    enjoy the tips
    thanks

  19. Levent Yilmaz on November 21, 2010 at 12:50 pm

    thx for great post…I have a question..how can I add numbers (1,2,3…10) like top ten chart listing? thanks for you help

    • c.bavota on November 22, 2010 at 11:07 am

      Of course. You can use an ordered list and the numbers will appear automatically.

    • Levent Yilmaz on November 25, 2010 at 5:30 pm

      thx for your answer… how can I use an ordered list ? can you show me please? sorry I m not a php expert…thanks for your help…

    • c.bavota on November 26, 2010 at 9:23 am

      You need to use <ol> instead of <ul> to wrap the function. Change that in the second block of HTML above.

  20. Levent Yilmaz on November 28, 2010 at 7:12 pm

    thanks very much bavota..cheers

  21. Shannon on December 10, 2010 at 3:21 am

    Works great and is very easy to use. Thank you!

  22. Nayan@Howtotechie on December 12, 2010 at 8:37 am

    Hello ,
    I have one question regarding this,currently you show popular posts order by comment desc,i have installled wp post views plugins,can you please help me how i show popular posts order by post views and comment desc.that means one posts which has 200 views and 20 comment and another posts contain 190 views and 25 comments so what will show ?

    • Abhishek on December 29, 2010 at 12:36 pm

      Great post!!!

      @nayan, this method only lets you order based on comments, views are “not” counted.

      -Abhi

  23. Marco on January 21, 2011 at 2:22 pm

    Might be late to the party but how do I add a CATEGORY to this function? Or better, how can I display the posts category? I sort of need that. :)

  24. servizio di traduzioni on February 28, 2011 at 2:03 pm

    Ottima risorsa :)
    Un saluto a tutti!

  25. phil on April 25, 2011 at 12:08 pm

    Do you have another code that lists the popular post based on the number of views. I think this would be more relevant as popular as it is the one being visited more often.

    • c.bavota on April 26, 2011 at 12:38 pm

      That would require a lot more code. You should look for a plugin.

    • merlin on May 22, 2011 at 1:52 am

      you need to use the plugin wp-postviews. this plugin counts the views of each post and stores it in a custom field. Then you can query the custom field “views” for ordering, instead of the comment count.