May
19
2009

How to List Your Most Popular Posts in WordPress


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.

If you liked this, please share it.

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

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

Discussion 22 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!

Leave a Reply

Your email address will not be published. Required fields are marked *

*


To enter code in the comment box, please place it between <pre lang="php"> </pre> tags. You don't have to convert any characters to their hex/HTML code. Just add your code the way you would to any code editor.