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

Go to source

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.