How to List Your Most Popular Posts in WordPress

Posted on May 19, 2009  |  Category: Tutorials

popular How to List Your Most Popular Posts in WordPressI 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 <?php and ?> 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.


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 How to List Your Most Popular Posts in WordPress 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.


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

Short URL: http://bavotasan.com/?p=576

15 Responses to “How to List Your Most Popular Posts in WordPress”

  1. 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.

    #2375
  2. 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.

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

    #3134
  4. Adi

    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

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

    Keep up the good work.

    #3325
  6. jayesh patel

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

    #3745
  7. 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?

    #4623
  8. 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/

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

    #7833
  10. 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.

    #7936
    • 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.

      #7959

Leave a Reply

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.