Highlighting the most popular posts on your site is a great way to send new visitors to your very best content and win them over.

There are a few different ways you can do this, and quite a few plugins out there to help. But why not base it on the number of comments? If you write a post that makes people talk, it must be a well written post (Most of the time…).

What We Will Do

What we’re going to do is quite simple, we will set up a query to get an ordered list of your most commented posts from your database, and then display those any way we like.

Popular Posts in The Sidebar

Setting this up as a sidebar widget is very easy. Just copy and paste the code below into your sidebar.php file.

To change the number of posts shown, just change the “5” at the end of line 3 to whatever number you would like.

Popular Posts

    get_results(“SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5”); foreach ($result as $post) { setup_postdata($post); $postid = $post->ID; $title = $post->post_title; $commentcount = $post->comment_count; if ($commentcount != 0) { ?>

  • {}

Set Up A Separate Popular Posts Page

Instead of simply having the list in your sidebar, you could create a longer list to show a lot more of your best posts, and put this list in its own page.

Method 1 – If You’re Using the Default Theme

If you are using the WordPress default theme, then the work is mostly done for you! Just download this file, popular-posts.txt  (Right Click > Save Link/Target As), then rename to popular-posts.php and upload it to your theme folder. Then skip on down this article to “Step 2 – Setting Up The Page.”

Method 2 – If You’re Using a Custom Theme

This is a little more complicated, but should still be quite straight forward.

1 – First off, open up your theme’s page.php file in a text editor (The WordPress editor won’t work here). If it doesn’t have one, then use the index.php file instead.

2 – Save this file into your theme folder (Same folder the page.php file came from!) with the new name, popular-posts.php.

3 – Paste the following code at the top of the popular-posts.php file:

Template Name: Popular Posts

4 – Look inside the file for a section of code that will look similar to the following. The first and last lines are the important ones. The code in between doesn’t matter too much (The code you see here is from my own page.php file. Yours will not be exactly the same!).

<!--?php if (have_posts()) : ?-->
<!--?php while (have_posts()) : the_post(); ?-->

<!--?php the_content('
Read the rest of this entry &amp;raquo;
'); ?&gt;
<!-- /post -->
<!--?php endwhile; else: ?-->

Sorry, no posts matched your criteria.

<!--?php endif; ?-->

Now, delete all of this code.

5 – In the place that you just deleted your code, paste the following instead: (Feel free to change the 5 in line 3 to any number you choose)

Popular Posts

<!--?php $result = $wpdb--->get_results("SELECT comment_count,ID,post_title FROM $wpdb-&gt;posts ORDER BY comment_count DESC LIMIT 0 , 5"); 
foreach ($result as $post) { 
$postid = $post-&gt;ID; 
$title = $post-&gt;post_title; 
$commentcount = $post-&gt;comment_count; 
if ($commentcount != 0) { ?&gt;


<!--?php } } ?-->

Now, in all likelihood, it will take some fiddling to get the layout right. Because your theme is custom made, there are going to be a few bits and pieces missing. Look at your page.php file and compare it to this code and try to copy over whatever is needed.

Step 2 – Setting Up The Page

Now go to your WordPress dashboard, and click Pages > Add New. Create a new page (The content in it doesn’t matter, it won’t be used) and then in the “Attributes” window, set the “Template” to “Popular Posts“. (The exact wording for those links is for WP2.7, but the Template option is still there in older versions).

Now, publish the page and go view it. You should see your most commented posts all listed with their comment counts beside them.

Using Custom Images

The code has been set up to include an image if you want to. When you publish your post, just add your image’s URL to a custom field called “popular_image” (Or change the popular_image name in the code to the name of a custom field you already use).

If you haven’t used custom fields before, you can read about them here. For all we are doing, it just means that on the post’s “Write” page, you go to the custom field’s section and type “popular_image” into the Key box, and the URL of the image into the Value box.

(I haven’t written much about custom fields here before. If you’d like to know a little more, just let me know in the comments and I’ll write up a post!)

Where to Next?

You have the code to get the posts. What you do with your page now is up to you. You can use CSS to style it any way you like. Have a look at the image below to see what milo has done with hers!

News _ milo

Have you tried this? Let us know if you run into any trouble with getting this working on your theme and we’ll see what we can do to help out!

milo WordPress Hacks Series: The code in these posts was written entirely by Milo, a web designer from Oxford, Britain. Check out her website for free WordPress themes and her blog, or follow her on Twitter!

Enjoy this post? You should follow me on Twitter!