
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.


















16 Responses
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();
Yes. It’s always better to use WordPress functions rather than query the database.
And when doing a query yourself then instead of “{$wpdb->prefix}posts” i believe it’s easier to use “$wpdb->posts”.
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
@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.
have_posts()) : ?>
have_posts()) : $popular->the_post(); ?>
<a href="”>
Do you eat the comments code. In the blog, write about WordPress, it is unacceptable.
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.
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.
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.
I agree with yasser could please explain me where to put that code?Waiting for ripply…
This is great and easy to implement, but howabout displaying the image attached to the post? Could that also be displayed?
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="”>
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: