How to list most commented posts with the new date queries in WordPress 3.7+

With the new WP_Date_Query in WordPress 3.7+ it’s easy to list most commented posts within a certain time span from a specific category.

Note: this example lists most commented articles posted within X date, not looking for most amount of comments within that time span among all articles.

We’re using WP_Query to list the posts here.

<?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; ?>

A whole lot simpler than before. 1 day ago, 1 week ago, 2 weeks ago, 1 month ago, 1 year ago, all time and so on – your choice. Note: for all time, the value would simply be 0.

7 Responses

  1. naser says:

    thank you for nice theme and useful wp post.
    well if put your post feed at least in code page 🙂

  2. This is so great! Awesome Article and very helpful !!!

  3. Thanks for your code. Before this, i’m using this code.

    Popular Posts by Most Commented

    get_results(”
    SELECT comment_count, ID, post_title
    FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 20
    “);

    foreach ($result as $post) {
    setup_postdata($post);
    $postid = $post->ID;
    $title = $post->post_title;
    $commentcount = $post->comment_count;
    if ($commentcount != 0) { ?>

    <a href="”>
    []

    But i think your code more neat, elegant and easy to understand. Peace.

  4. Phil says:

    I searched high and low for something like this, thanks for sharing!

  5. Douglas says:

    HI, how to get only posts with 1 or more comments?

Leave a Reply to Naziman Azlye Cancel reply

Your email address will not be published. Required fields are marked *