I managed to figure it out. So, get_posts() uses WP_Query() and we can use the example in the documentation to filter the dates.
Now for the comments, that is easy, we just add the parameter 'orderby'=>'comment_count'
. Also to note, when filtering get_posts(), we will need to disable suppress_filters
.
Let’s filter the time first:
function filter_where( $where="" ) {
// posts in the last 2 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-2 days')) . "'";
return $where;
}
And now let’s get the posts:
add_filter( 'posts_where', 'filter_where' );
$query = get_posts(
array (
'numberposts' => 2,
'orderby'=>'comment_count',
'order'=>'ASC',
'suppress_filters' => false,
'post_type' => array ( 'post' )
)
);
remove_filter( 'posts_where', 'filter_where' );
That will give you the top least commented posts from the past 2 days.