Using WordPress ‘comment_feed_groupby’ PHP filter

The comment_feed_groupby WordPress PHP filter allows you to modify the GROUP BY clause of the comments feed query before it’s executed.

Usage

add_filter('comment_feed_groupby', 'your_custom_function', 10, 2);

function your_custom_function($cgroupby, $query) {
  // your custom code here
  return $cgroupby;
}

Parameters

  • $cgroupby (string) – The GROUP BY clause of the query.
  • $query (WP_Query) – The WP_Query instance (passed by reference).

More information

See WordPress Developer Resources: comment_feed_groupby

Examples

Group comments by post ID

Group comments in the feed by their associated post ID.

add_filter('comment_feed_groupby', 'group_comments_by_post_id', 10, 2);

function group_comments_by_post_id($cgroupby, $query) {
  $cgroupby = 'comment_post_ID';
  return $cgroupby;
}

Group comments by author email

Group comments in the feed by the author’s email address.

add_filter('comment_feed_groupby', 'group_comments_by_author_email', 10, 2);

function group_comments_by_author_email($cgroupby, $query) {
  $cgroupby = 'comment_author_email';
  return $cgroupby;
}

Group comments by status

Group comments in the feed by their status (approved, pending, etc.).

add_filter('comment_feed_groupby', 'group_comments_by_status', 10, 2);

function group_comments_by_status($cgroupby, $query) {
  $cgroupby = 'comment_approved';
  return $cgroupby;
}

Group comments by date (Year and Month)

Group comments in the feed by the year and month of their submission.

add_filter('comment_feed_groupby', 'group_comments_by_date', 10, 2);

function group_comments_by_date($cgroupby, $query) {
  global $wpdb;
  $cgroupby = "YEAR({$wpdb->comments}.comment_date), MONTH({$wpdb->comments}.comment_date)";
  return $cgroupby;
}

Remove GROUP BY clause from comments feed query

Remove the GROUP BY clause from the comments feed query to display all comments without grouping.

add_filter('comment_feed_groupby', 'remove_groupby_from_comments_feed', 10, 2);

function remove_groupby_from_comments_feed($cgroupby, $query) {
  $cgroupby = '';
  return $cgroupby;
}