We always say that comments are important. You might think one article is more important than the other one, but the comments will be the judge of that. In most cases, the more comments a post has it means the more popular that post is.
So if you’re trying to display the most popular posts on your site, it would be a good idea to sort them by comment count.
In some cases, you will want to show only most commented articles from specific year. For example, when the end of a year is near, you might want to summarize your blog for your readers and show them which articles have had the most comments for that particular year.
It will only take you few minutes to set up everything, you won’t be needing additional plugins and your readers (especially those who contributed with their comments) will appreciate your effort and will find the list interesting.
In the following lines, we are about to show you the snippet which will sort the posts for you; the only thing you have to define is the year. If you leave out the year parameter, the code will go through the entire posts database:
- Open sidebar.php or any file where you want your list to appear
- Copy and paste the following code:
- Change year on the 2nd line
- Change number of post to be displayed on 4th line
- Save changes
$args = array( 'year' => 2015, 'orderby' => 'comment_count', 'posts_per_page' => 10, 'ignore_sticky_posts' => 1, ); $wp_query = new WP_Query( $args ); if ( $wp_query->have_posts() ) : echo "<ul>"; while ( $wp_query->have_posts() ) : $wp_query->the_post(); echo '<li><a href="' . get_the_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a> (' . get_comments_number( '0', '1', '%' ) . ')</li>'; endwhile; echo "</ul>"; else :
get_template_part( ‘content’, ‘none’ );
If you’re using this function just to show the most commented posts for the last year, you might not be interested in showing the list on the sidebar. Instead, you can only create the list as part of one article. In that case, see how to make the PHP code work directly from your text editor.
Don’t forget to add styles to your list and enjoy your most commented articles – we hope there are a lot of them.