钩子文档

comment_feed_limits

💡 云策文档标注

概述

comment_feed_limits 是一个 WordPress 过滤器,用于在发送评论源查询前修改其 LIMIT 子句。它允许开发者自定义评论源中返回的评论数量,默认限制为 10 条。

关键要点

  • 过滤器名称:comment_feed_limits
  • 作用:过滤评论源查询的 LIMIT 子句,控制返回的评论数量
  • 参数:$climits(字符串,查询的 LIMIT 子句)和 $query(WP_Query 实例,按引用传递)
  • 默认值:基于 get_option('posts_per_rss') 设置,通常为 10
  • 引入版本:WordPress 2.8.0
  • 相关函数:在 WP_Query::get_posts() 中调用

代码示例

// 将评论源中的评论数量从默认 10 条增加到 100 条(PHP 5.2 或更早版本)
function wpdocs_raise_comment_feed_limit( $limit, &$instance ) {
    return 'LIMIT 100';
}
add_filter( 'comment_feed_limits', 'wpdocs_raise_comment_feed_limit', 10, 2 );

// 将评论源中的评论数量从默认 10 条增加到 100 条(PHP 5.3 或更高版本,使用匿名函数)
add_filter( 'comment_feed_limits', function( $a ) { return "LIMIT 100"; } );

📄 原文内容

Filters the LIMIT clause of the comments feed query before sending.

Parameters

$climitsstring
The JOIN clause of the query.
$queryWP_Query
The WP_Query instance (passed by reference).

Source

$climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) );

Changelog

Version Description
2.8.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Raise the number of comments retrieved in comment feeds from the default (10) to 100:

    In PHP 5.2 or earlier:

    /**
     * Filter the LIMIT clause on comment feed queries.
     *
     * @param string   $limit    LIMIT clause for the comment feed query.
     * @param WP_Query $instance WP_Query instance, passed by reference.
     * @return string (maybe) Filtered comment feed query LIMIT clause.
    function wpdocs_raise_comment_feed_limit( $limit, &$instance ) {
    	return 'LIMIT 100';
    }
    add_filter( 'comment_feed_limits', 'wpdocs_raise_comment_feed_limits', 10, 2 );

    In PHP 5.3 or later:

    // Filter the LIMIT clause on comment feed queries to increase the number from 10 to 100.
    add_filter( 'comment_feed_limits', function( $a ) { return "LIMIT 100"; } );