Post Pic

WordPress hack: Get popular posts by comments count

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

Simply paste the following code where you want to display your most popular posts to be displayed:

$pop = $wpdb->get_results("SELECT id, post_title, comment_count FROM {$wpdb->prefix}posts WHERE post_type='post' ORDER BY comment_count DESC LIMIT 10");

<ul>
foreach($pop as $post) : ?>
<li> <?php echo $post->post_title; ?> </li>
<?php endforeach; ?>
</ul>

Thanks to Neil Skoglund for this nice piece of code!

16 Responses

May 11 2010 16:26

I think this is better:

query_posts(array(‘orderby’ => ‘comment_count’, ’showposts’ =>10 ));

if (have_posts()) : while (have_posts()) : the_post();
html here
endwhile; endif;

wp_reset_query();

May 11 2010 17:34

Yes. It’s always better to use WordPress functions rather than query the database.

May 11 2010 17:53

And when doing a query yourself then instead of “{$wpdb->prefix}posts” i believe it’s easier to use “$wpdb->posts”.

May 12 2010 06:43

Hello

could please explain me where to put that code?
because, I have put it in widget text in sidebar and it shows the code and nothing else.

thanks for your help

May 12 2010 16:08

@Yasser, wrap the first line with <?php the_line_here ?> and add <?php before “foreach($pop as $post) : ?>”. That should work, seems Jean made a little mistake.

May 13 2010 14:36

have_posts()) : ?>

have_posts()) : $popular->the_post(); ?>
<a href="”>

May 13 2010 14:37

Do you eat the comments code. In the blog, write about WordPress, it is unacceptable.

May 13 2010 15:56

I don’t fully understand why people deem a post is popular because of how many comments it has. Sure, this may be the case sometimes, but I’ve seen way to many posts that have tons of comments, but are typically a back and forth conversation between 2 people.

I think it would make more sense to base the popular post on how many times it was viewed. Lester Chan’s WP-PostViews plugin would be perfect for that.

May 14 2010 15:28

Troy i completely agree with you about that, but the thing is that WordPress doesn’t count views itself, it needs a plugin (WP-PostViews) to do that. When i started wpcanyon.com i installed WP-PostViews at the same time but not a lot of people do that so showing popularity by comment count is the only way to do it for them.

May 19 2010 23:56

That’s helpful, thank you.
I have been interested in seeing whether I could add this into my blog, but then I figure it’s probably best by amount times viewed.
I use the WP Popular Posts Stats for this, as it shows you in the dashboard which posts are most popular daily, weekly, monthly and all time. Plus it incorporates a widget, where you can display your most popular posts based on which timeframe you wish. I figure it’s more integrated, than having one widget which does only one thing.

May 31 2010 21:54

I agree with yasser could please explain me where to put that code?Waiting for ripply…

Aug 04 2010 15:14

This is great and easy to implement, but howabout displaying the image attached to the post? Could that also be displayed?

Aug 11 2010 20:53

Do that:

Global, using standard WordPress functions and hooks – the_permalink, the_title and WP_Query.

Without image:

have_posts()) : $popular->the_post(); ?>

<a href="”>

With an image(using timthumb and proper fall-back):

have_posts()) : $popular->the_post(); ?>

ID, ‘postthumb’, true);

if (has_post_thumbnail()) { ?>

‘post-thumb’)); ?>

<a href="”>

<img src="/timthumb.php?src=ID, ‘postthumb’, true); ?>&h=50&w=50&zc=1″ alt=”" width=”50″ height=”50″ />

<a href="”>

<img src="/images/blank.png” alt=”No Preview Available” width=”50″ height=”50″ />

<a href="”>

Aug 17 2010 19:41

I do not see in this code getting a link and making the title a link to the post. Shouldn’t that be more useful :)

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required