Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Chrico/7233135 to your computer and use it in GitHub Desktop.
Save Chrico/7233135 to your computer and use it in GitHub Desktop.
Comment Filter to fetch comments from last week with new WP_Date_Query()
<?php
/**
* adding a date-filter to the comment_query to fetch comments from last week
*
* @param Array $clauses
* @return Array $clauses
*/
function chrico_filter_comments_clauses_get_comments_last_week( $clauses ){
$last_week = gmdate( 'W' ) - 1;
$query_args = array(
'w' => $last_week
);
$date_query = new \WP_Date_Query( $query_args, 'comment_date' );
$clauses['where'] .= $date_query->get_sql();
return $clauses;
}
add_filter('comments_clauses', 'chrico_filter_comments_clauses_get_comments_last_week' );
$comments = get_comments();
remove_filter( 'comments_clauses', 'chrico_filter_comments_clauses_get_comments_last_week' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment