Using WordPress ‘comment_feed_limits’ PHP filter

The comment_feed_limits WordPress PHP filter modifies the LIMIT clause of the comments feed query before sending.

Usage

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

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

Parameters

  • $climits (string) – The LIMIT clause of the query.
  • $query (WP_Query) – The WP_Query instance (passed by reference).

More information

See WordPress Developer Resources: comment_feed_limits

Examples

Limit the number of comments in the feed to 5

This code sets the maximum number of comments displayed in the feed to 5.

add_filter('comment_feed_limits', 'limit_comment_feed', 10, 2);

function limit_comment_feed($climits, $query) {
    $climits = 'LIMIT 5';
    return $climits;
}

Remove the limit from the comments feed

This code removes the limit on the number of comments displayed in the feed.

add_filter('comment_feed_limits', 'remove_limit_comment_feed', 10, 2);

function remove_limit_comment_feed($climits, $query) {
    $climits = '';
    return $climits;
}

Limit comments feed based on a category

This code limits the comments feed to 10 comments if the category is ‘news.’

add_filter('comment_feed_limits', 'limit_comments_for_news_category', 10, 2);

function limit_comments_for_news_category($climits, $query) {
    if (is_category('news')) {
        $climits = 'LIMIT 10';
    }
    return $climits;
}

Limit comments feed based on post author

This code limits the comments feed to 8 comments if the post author’s ID is 1.

add_filter('comment_feed_limits', 'limit_comments_for_author', 10, 2);

function limit_comments_for_author($climits, $query) {
    if ($query->get('author') == 1) {
        $climits = 'LIMIT 8';
    }
    return $climits;
}

Increase comments limit for logged-in users

This code increases the comments feed limit to 15 for logged-in users.

add_filter('comment_feed_limits', 'increase_comments_limit_for_logged_in_users', 10, 2);

function increase_comments_limit_for_logged_in_users($climits, $query) {
    if (is_user_logged_in()) {
        $climits = 'LIMIT 15';
    }
    return $climits;
}