How to List Your Most Popular Posts in WordPress
by c.bavota | Posted in Tutorials | 42 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
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.



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.
Nice tip on adding a couple of filters on the query. Thanks Christopher.
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.
Very helpful tutorial. Thanks a lot for this one. Already implemented this on my homepage.
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
I am trying to figure out how to use hits as well. As soon as I do you I will post it.
still waiting for this tutorial ..
Thanx for this, already added it to my new project.
Keep up the good work.
that’s a good posting because we don’t need any plugin for showing popular post.
good posting..
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?
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/
Wow, this one is pretty clear and simple! I used some Plugins but they do not give me the really most popular Topics out.
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.
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.
good post, ths
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.
Clear and simple code.
But I still prefer plugins. One of the best WordPress features is indeed plugin support. What if you change theme?
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!
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?
Very nice chunk of code! I’ll use it!
WELL happy with this bit of code. Really easy to implement and gives me complete control. Thank you!
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
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
Of course. You can use an ordered list and the numbers will appear automatically.
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…
You need to use <ol> instead of <ul> to wrap the function. Change that in the second block of HTML above.