1

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.

1

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" ),
        ),
    ),
) );
  • Nice, had no idea that feature had been added! Now, I can possibly figure this out in several hours with the tiny php knowledge I have - but if you have time, could you type together a quick example of how the query would look, with for example values like showposts=6&cat=mycat&ignore_sticky_posts=1&orderby=comment_count and with most comments from 1 week ago - or if possible, X days ago? Been looking at it since you responded, but I'm not quite sure how to apply it. – Alx Oct 30 '13 at 10:17
  • I'm not sure if it will work like this. You might have to tweak that date stuff, but it should be enough to give you an idea to get around it. – kaiser Oct 30 '13 at 10:36
  • Thank you for taking the time to write the example. I edited my first post with a code that works for me. – Alx Oct 30 '13 at 13:09
  • @AlexanderA. Please don't put solutions in questions. Either edit my answer or add another one of your own. Thanks. :) – kaiser Oct 30 '13 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.