I’ve removed my old post here, as the posts_where method is outdated.
Thanks to kaiser, this is now how easy it is to list most commented posts within a certain time span:
<?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; ?>
Note: this sorts by most commented articles posted within X date, not checking all articles and then looking for most amount of comments within that time span among all articles. No idea how to do the second one, or how heavy it would be.
Where?
The
WP_Comment_Query
class has the following filter for its clauses:That means you can hook into
comments_clauses
and filter the$clauses['where']
part.How?
Since WP 3.7, we got a
WP_Date_Query
class. It works like the following (simplified examples):The default values that are used during
WP_Query
– retrieved via query vars:How to do a
date_query
: Code example copied from hereYou can as well do a plain
WP_Date_Query()
and fetch the resulting SQL string for later use in clauses filters:Now Same source a date query for a post and its comments:
Mocked up
Not tested …