函数
$args = array( 'author_email' => '', 'author__in' => '', 'author__not_in' => '', 'include_unapproved' => '', 'fields' => '', 'ID' => '', 'comment__in' => '', 'comment__not_in' => '', 'karma' => '', 'number' => '', 'offset' => '', 'orderby' => '', 'order' => 'DESC', 'parent' => '', 'post_author__in' => '', 'post_author__not_in' => '', 'post_ID' => '', // ignored (use post_id instead) 'post_id' => 0, 'post__in' => '', 'post__not_in' => '', 'post_author' => '', 'post_name' => '', 'post_parent' => '', 'post_status' => '', 'post_type' => '', 'status' => 'all', 'type' => '', 'type__in' => '', 'type__not_in' => '', 'user_id' => '', 'search' => '', 'count' => false, 'meta_key' => '', 'meta_value' => '', 'meta_query' => '', 'date_query' => null, // See WP_Date_Query ); get_comments( $args );
描述
get_comments()
是可以在主循环外获取最新评论列表的 WordPress 函数,通过 get_comments()
函数可以获取整站的最新评论,通过设置相关参数还可以实现获取指定文章、指定用户、指定 ID 或指定邮箱的评论,get_comments()
函数可以返回评论 ID、评论的文章 ID、评论用户、评论邮箱、评论内容等信息。比如要在首页调用文章 ID 为 1 的评论,通过该函数就可以轻易实现。
参数
$author_email
– (字符串)评论用户的邮件地址,默认为空。
$author__in
– (数组)包含指定用户 ID 的评论,默认为空。
$author__not_in
– (数组)排除指定用户 ID 的评论,默认为空。
$comment__in
– (数组)获取指定ID的评论,默认为空。
$comment__not_in
– (数组)排除指定 ID 的评论,默认为空。
$count
– (布尔型)返回评论的数量值,默认为 false。
$date_query
– (数组)返回限制时间段的评论,或参考 WP_Date_Query
,默认为空。
$fields
– (字符串)返回评论的字段,仅限注释ID的“ids”或空,默认为空。
$ID
– (整型)暂未被使用?(官网注释 Currently unused
.),默认为空。
$include_unapproved
– (数组)包含未被审核的评论ID或邮箱地址,默认为空。
$karma
– (整型)用于检索匹配注释的 KARMA 得分,默认为空。
$meta_key
– (字符串)包含的评论自定义字段,默认为空。
$meta_value
– (字符串)包含的评论自定义字段值,必须和 $meta_key
一起使用,默认为空。
$meta_query
– (数组)自定义字段查询条件,参考 WP_Meta_Query
函数,默认为空。
$number
– (整型)返回的评论条数,不设置则不限量数量,默认为空。
$offset
– (整型)从第几条开始的评论,默认为空。
$orderby
– (字符串数组)返回的评论排序方式,如果根据“meta_value”或“meta_value_num”排列,那么必须设置 $meta_key 参数;如果使用meta_query查询,那么必须使用它的数组值。支持的排序方式:comment_agent、comment_approved、comment_author、comment_author_email、comment_author_IP、comment_author_url、comment_content、comment_date、comment_date_gmt、comment_ID、comment_karma、comment_parent、comment_post_ID、comment_type、user_id、meta_value、meta_value_num、the value of $meta_key、and the array keys of $meta_query、Also accepts false, an empty array, or ‘none’ to disable ORDER BY clause,默认为comment_date_gmt。
$order
– (字符串)排序顺序,可选 ASC 或 DESC,默认 DESC。
$parent
– (整型)指定 ID 的子评论,默认为空。
$post_author__in
– (数组)获取指定文章作者 ID 的评论,默认为空。
$post_author__not_in
– (数组)排除指定文章作者 ID 的评论,默认为空。
$post_ID
– (整型)(官网注释 Currently unused),默认为空。
$post_id
– (整型)指定单篇文章 ID,默认为空。
$post__in
– (数组)指定多篇文章 ID 的评论,默认为空。
$post__not_in
– (数组)排除指定文章 ID 的评论,默认为空。
$post_author
– (整型)指定文章作者 ID 的评论,默认为空。
$post_status
– (字符串)文章的状态,草稿、待审或者已发布,默认为空。
$post_type
– (字符串)文章类型,如 post、page 或自定义文章类型,默认为空。
$post_name
– (字符串)文章名称,默认为空。
$post_parent
– (整形)指定父 ID 的评论,默认为空。
$search
– (字符串)搜索匹配,默认为空。
$status
– (字符串)评论的状态,支持“hold”、“approve”、“all”或自定义的评论状态,默认为all。
$type
– (字符串数组)评论的类型,可选“comment”、“pings”(包含“pingback”和“trackback”),或自定义类型,默认为空。
$type__in
– (数组)评论的类型,多个类型使用,默认为空。
$type__not_in
– (数组)要排除的评论类型,默认为空。
$user_id
– (整型)指定会员 ID 的评论。
返回值
函数的返回值是数组,数组包含以下字段。
comment_ID
– 评论ID
comment_post_ID
– 评论父ID
comment_author
– 评论用户名
comment_author_email
– 评论用户邮箱
comment_author_url
– 评论用户网址
comment_author_IP
– 评论用户 IP
comment_date
– 评论时间,格式(YYYY-MM-DD HH:MM:SS)
comment_date_gmt
– 评论的GMT时间(YYYY-MM-DD HH:MM:SS)
comment_content
– 评论内容
comment_karma
– 评论的karma
comment_approved
– 评论状态(0,1或“spam”)
comment_agent
– 评论用户的工具(浏览器、操作系统等信息)
comment_type
– 评论的类型(pingback或trackback),空的话表示常规评论
comment_parent
– 嵌套评论的父评论(0为顶层)
user_id
– 用户ID(如果评论的用户是网站注册用户则返回)
例子
调用文章 ID 为 15 的所有评论
$comments = get_comments('post_id=15'); foreach($comments as $comment) : echo($comment->comment_author); endforeach;
调用文章 ID 为 1 的 5 条未审核的评论
$args = array( 'status' => 'hold', 'number' => '5', 'post_id' => 1, // use post_id, not post_ID ); $comments = get_comments($args); foreach($comments as $comment) : echo($comment->comment_author . '<br />' . $comment->comment_content); endforeach;
显示文章 ID 为 1 的评论的数量
$args = array( 'post_id' => 1, // use post_id, not post_ID 'count' => true //return only the count ); $comments = get_comments($args); echo $comments
调用注册 ID 为 1 的用户评论的数量
$args = array( 'user_id' => 1, // use user_id 'count' => true //return only the count ); $comments = get_comments($args); echo $comments
调用注册 ID 为 1 的用户的评论
$args = array( 'user_id' => 1, // use user_id ); $comments = get_comments($args); foreach($comments as $comment) : echo($comment->comment_author . '<br />' . $comment->comment_content); endforeach;
调用最近四周的评论
$args = array( 'date_query' => array( 'after' => '4 week ago', 'before' => 'tomorrow', 'inclusive' => true, ), ); $posts = get_comments($args); foreach ($posts as $post) { // Output comments etc here }
删除同一篇文章同一个人的重复评论
// get all approved comments with empty number arg $all_comments=get_comments( array('status' => 'approve', 'number'=>'') ); // array to hold comment ids that are dupes $comment_ids_to_delete=array(); foreach($all_comments as $k=>$c) { $kk=($k-1); // the previous comments index in all_comments array $pc=$all_comments[$kk]; // the previous comment object // if comment authors the same, and comment_content the same add to deletions array if($pc->comment_author == $c->comment_author && $pc->comment_content == $c->comment_content) { $comment_ids_to_delete[]=$pc->comment_ID; // previous comment id } } // delete the comment by id foreach($comment_ids_to_delete as $k=>$v): wp_delete_comment($v); endforeach;
源文件
wp-includes/comment.php
官方文档
本文由 猫斯基 原创发布。
著作权均归用户本人所有。独家文章转载,请联系本站管理员。获得授权后,须注明本文地址! 本文地址:https://maosiji.com/wordpress/wordpress-get_comments.html