Display Most Commented Posts From 2013

[ad_1]

A popular feature for the sidebar of your WordPress blog is to display most popular blog posts. Probably you have seen this on another blog and you might be wondering how it is done. There are many WordPress plugins that can do this but in this article we will show you how to display 5 most popular posts from year 2013 without a plugin. We will be identifying popular posts based on how many comments a post has. Effectively we will be displaying most commented posts from year 2013.

Earlier we have shown you how to display related posts and latest sticky posts in WordPress. Now in this code snippet, we will run a WordPress query to get all blog posts from year 2013 and sort them in the descending order by comment counts. You can also define how many posts you want to display, in this particular example we will be returning 5 most commented posts. We will display the title of the post as well as comment count so the visitor will be able to see how popular this post is. The beauty of using WordPress query is that it is quick, easy and highly customization to just add this into your theme and then you can style it exactly as you want.

Here is the code snippet for displaying most commented posts from year 2013. You can use this code snippet in your theme, preferably in sidebar of your website.

<?php
// display most commented posts from 2013
$args = array(
    'year'  => 2013,
    'orderby' => 'comment_count',
    'posts_per_page' => 5,
    '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' );
endif;

wp_reset_postdata();
?>

If you remove 'year' => 2013, argument from above code snippet then it will display most commented posts from the beginning of your blog.

[ad_2]

Leave a Reply

Random Post Selection ::

Recent Posts

Unlock Unlimited Downloads