WordPress Popular Posts by Comments
Displaying most commented posts in your WordPress website is not a bad idea.Many shows popular posts in sidebar based on views,categories and tags.There are plenty of WordPress plugins available to do this work.We recommends to do not use plugins unless,if you don't have alternate method i.e code snippets.
The following code runs SQL query to display most popular posts in WordPress by comments.You can also instruct how many posts to display and where to display these most commented posts.If you want to show in sidebar then add code to your themes sidebar.php function.It's totally your choice to display in anywhere in WordPress website.
<ul class="popular_posts">
<?php $pc = new WP_Query('orderby=comment_count&posts_per_page=10');
while ($pc->have_posts()) : $pc->the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<p>Posted by <strong><?php the_author() ?></strong> with <?php comments_popup_link('No Comments;', '1 Comment', '% Comments'); ?></p></li>
<?php endwhile; ?>
</ul>
The above code shows most commented posts titles with total number of comments count,which is very helpful to visitors to the popularity of post.Finally add the CSS styles to your style sheet to display attractively even more.
Display Most commented Posts by Category in WordPress
Form WordPress 4.0 and above versions with WP_Date_Query, you can display most commented posts in WordPress by category and date.You can select specific category ,total number of posts within the time span.
Add following code to display most commented popular posts by category in your wordpress for all time.In this cod 0 indicates that for all time to show most commented posts.
<?php
$popular = new WP_Query( array(
'post_type' => array( 'post' ),
'showposts' => 6,
'cat' => 'MyCategory',
'ignore_sticky_posts' => true,
'orderby' => 'comment_count',
'order' => 'dsc',
'date_query' => array(
array(
'after' => '0',
),
),
) );
?>
<?php while ( $popular->have_posts() ): $popular->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
display most commented posts in wordpress by category for 1 week with the following code snippet
<?php
$popular = new WP_Query( array(
'post_type' => array( 'post' ),
'showposts' => 6,
'cat' => 'MyCategory',
'ignore_sticky_posts' => true,
'orderby' => 'comment_count',
'order' => 'dsc',
'date_query' => array(
array(
'after' => '1 week ago',
),
),
) );
?>
<?php while ( $popular->have_posts() ): $popular->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
If you want to change time span,simply change 1 week ago in above code.
Ex : for 1 month use 1 month ago.
For 2 months use 2 months ago
For 1 day use 1 day ago
For 1 year use 1 year ago
for 3 weeks use 3 weeks ago
Finally don't forget to change My category with your category name.