Where?
The WP_Comment_Query
class has the following filter for its clauses:
$pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' );
$clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
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:
array(
'year' => '',
'monthnum' => '',
'week' => '',
'day' => '',
'hour' => '',
'minute' => '',
'second' => '',
)
How to do a date_query
: Code example copied from here
$dateQuery = new WP_Query( array(
'date_query' => array(
'column' => 'optional, column to query against, default is post_date',
'compare' => 'optional, see WP_Date_Query::get_compare()',
'relation' => 'optional, OR or AND, how the sub-arrays should be compared, default is AND',
array(
'column' => 'see above',
'compare' => 'see above',
'after' => 'string or array, see WP_Date_Query::build_mysql_datetime()',
'before' => 'string or array, see WP_Date_Query::build_mysql_datetime()',
'inclusive' => 'boolean, for after/before, whether exact value should be matched or not',
'year' => '4 digit int',
'month' => 'int, 1-12',
'week' => 'int, 0-53',
'day' => 'int, 1-31',
'hour' => 'int, 0-23',
'minute' => 'int, 0-60',
'second' => 'int, 0-60',
),
),
// ... other query args
) );
You can as well do a plain WP_Date_Query()
and fetch the resulting SQL string for later use in clauses filters:
$dateQuery = WP_Date_Query( array( /* your query */ ) );
$whereDate = $dateQuery->get_sql();
// Inspect result
var_dump( $whereDate );
Now Same source a date query for a post and its comments:
// All comments that are within the past week
$some_comments = get_comments( array(
'post_ID' => get_the_ID(), // Can be omited as well
'date_query' => array(
array(
'after' => '1 week ago',
),
),
'count' => true // return only the comment count
) );
Mocked up
Not tested …
global $wp_query;
$daysQuery = new WP_Query( array(
'post_type' => array( 'post' ),
'showposts' => 6,
'cat' => 'mycat',
'ignore_sticky_posts' => true,
'orderby' => 'comment_count date',
'date_query' => array(
array(
'after' => array(
'year' => date( "Y" ),
'month' => date( "m" ),
'day' => "01",
),
'before' => array(
'year' => date( "Y" ),
'month' => date( "m", strtotime( "-1 Months" ) ),
'day' => date( "t", strtotime( "-1 Months" ) ),
),
// 'inclusive' => true,
'month' => date( "m" ),
),
),
) );