Using WordPress ‘comment_feed_orderby’ PHP filter

The comment_feed_orderby WordPress PHP filter allows you to modify the ORDER BY clause of the comments feed query before it is executed.

Usage

add_filter('comment_feed_orderby', 'your_custom_function', 10, 2);
function your_custom_function($corderby, $query) {
    // your custom code here
    return $corderby;
}

Parameters

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

More information

See WordPress Developer Resources: comment_feed_orderby

Examples

Order comments feed by date (ascending)

Order comments feed by the comment date in ascending order.

add_filter('comment_feed_orderby', 'order_comments_feed_by_date_asc', 10, 2);
function order_comments_feed_by_date_asc($corderby, $query) {
    $corderby = "comment_date ASC";
    return $corderby;
}

Order comments feed by author

Order comments feed by the author’s display name.

add_filter('comment_feed_orderby', 'order_comments_feed_by_author', 10, 2);
function order_comments_feed_by_author($corderby, $query) {
    global $wpdb;
    $corderby = "{$wpdb->comments}.comment_author ASC";
    return $corderby;
}

Order comments feed by comment content length

Order comments feed by the length of the comment content.

add_filter('comment_feed_orderby', 'order_comments_feed_by_content_length', 10, 2);
function order_comments_feed_by_content_length($corderby, $query) {
    global $wpdb;
    $corderby = "LENGTH({$wpdb->comments}.comment_content) ASC";
    return $corderby;
}

Order comments feed by comment karma

Order comments feed by the comment karma.

add_filter('comment_feed_orderby', 'order_comments_feed_by_karma', 10, 2);
function order_comments_feed_by_karma($corderby, $query) {
    global $wpdb;
    $corderby = "{$wpdb->comments}.comment_karma DESC";
    return $corderby;
}

Order comments feed by random order

Order comments feed in a random order.

add_filter('comment_feed_orderby', 'order_comments_feed_by_random', 10, 2);
function order_comments_feed_by_random($corderby, $query) {
    $corderby = "RAND()";
    return $corderby;
}