pre_get_comments
云策文档标注
概述
pre_get_comments 是一个 WordPress Hook,在评论被检索之前触发,允许开发者修改 WP_Comment_Query 实例以自定义评论查询。
关键要点
- Hook 名称:pre_get_comments
- 触发时机:在评论被检索之前
- 参数:$query(WP_Comment_Query 实例,通过引用传递)
- 用途:用于修改评论查询条件,如过滤或排序评论
- 相关函数:WP_Comment_Query::get_comments()
- 引入版本:WordPress 3.1.0
代码示例
add_action('pre_get_comments', 'custom_pre_get_comments');
function custom_pre_get_comments($query) {
// 修改查询参数,例如只显示特定状态的评论
$query->query_vars['status'] = 'approve';
}注意事项
- 参数 $query 是 WP_Comment_Query 实例,通过引用传递,可以直接修改其属性。
- 此 Hook 在 WP_Comment_Query::get_comments() 方法中被调用,用于获取匹配查询变量的评论列表。
- 确保在修改查询参数时遵循 WordPress 的评论查询规范,以避免意外行为。
原文内容
Fires before comments are retrieved.
Parameters
$queryWP_Comment_Query-
Current instance of WP_Comment_Query (passed by reference).
Source
do_action_ref_array( 'pre_get_comments', array( &$this ) );
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |