Quick Tip: “Popular Posts By Comment Count” SQL Query in WordPress

MySQL DB

You might have noticed that the Tuts+ sites have a section on the home page where we list the most popular posts of the month, according to comment count. While there are numerous plugins available, it’s always best to write the code yourself if you can. Too much abstraction is never a good thing! Luckily, once we learn how to query WordPress’ database, tasks like this become a cinch!

Subscribe to our YouTube page to watch all of the video tutorials!
Prefer to watch this video on Screenr?

The SQL Query
To query WordPress’ database, we can use the $wpdb->get_results() method. As the parameter, we simply pass a SQL query. In this particular example, let’s say that we only want to display a list of popular posts within our sidebar. We can use the following query:

$pop = $wpdb->get_results(“SELECT id, post_title, comment_count FROM {$wpdb->prefix}posts WHERE post_type=’post’ ORDER BY comment_count DESC…

You Might Also Like...

  • WordPress hack: Get popular posts by comments count

    2737 readersIn blogging, it is useful to be able to display your most popular posts. There's WordPress plugins to do that, but you don't need it: This code will display your most popular posts, accordoing to the comments count, without requiring you to use a plugin.Looking for WordPress hosting? Try WP Web Host. Prices starts at

  • How To: Displaying Your Most Commented Posts

    221 readersThough this isn’t quite the same as displaying your most popular posts in your sidebar, here is some code you can use to display the posts that have received the most comments.  You’ll want to place it in your sidebar where you want the code to be displayed. Most Commented Posts Code Go into your theme files

  • Display most recent comments with Gravatar

    1582 readersIs your blog popular? Do you got receive lots of comments from your readers? If yes, what about displaying the most recent comments in your blog sidebar (or elsewhere) to let your visitors knowing about the discussion?Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!Display

  • Most Commented Posts The Right Way in WordPress

    1419 readersJust a very quick tip but nonetheless an important one. Many of you will be using any number of plugins or the database query that's been doing the rounds recently to display your most popular posts. That code/plugin is no longer needed as WordPress' query_posts can now display your most commented posts.You really should be

  • ;)

    WordPress hack: Show your top contributors without a plugin

    2349 readersDo you ever wished to be able to list your top contributors without using a plugin? If yes, you'll be happy with this recipe that will show you how to do it. Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress hack: Show your top

  • How to Create a Comments Central Page Template in WordPress

    321 readersOne way to increase visitor engagements is to reward their comments by showcasing them on your website. Additionally, you can also feature the top commenters as well, linking back to their website in the process. Here we’ll create a dedicated Page Template to display those comments and commenters in one place. In short, this tutorial

  • The finished widgets

    How to Write a “Most Popular By Views” WordPress Plugin

    2809 readersAs you continue writing for WordPress more and more, the level of difficulty and complexity of your plugins will eventually reach far beyond the simple back-end data manipulation, as demonstrated in our beginner’s article. In this installment we’ll cover in-depth: database interaction, Admin Widgets, and Sidebar Widgets. Today’s example will be a custom built “Most

  • List Posts by Category In Navigation

    116 readersWordPress is primarily a blog platform. Not infrequently it is used as a CMS. But not just static pages play a role, but also posts, because they are giving more opportunities, having better performance and be published in feeds. Therefore, I prefer to use, in the context of WP as a CMS, posts more than

  • How to display an incrementing number next to each published post

    1783 readersEver wished to be able to automatically number your posts (Like Article 1, Article 2, and so on) like sites such as A List Apart are doing? If yes, just read this recipe which will show you how to do it easily.Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you

  • chart

    Creating a Chart on Your WordPress Archives Page

    356 readersOne part of WordPress that I've never been overly impressed with is archives. This isn't so much a WordPress problem as it an archive problem, archives are inherently boring. As a result of this revelation I've spent some time thinking up new ways to present the information. My latest idea was adding charts to represent