WordPress获取评论列表函数:get_comments

WordPress函数get_comments用于获取整个网站或指定文章的评论列表。

get_comments( string|array $args = ” ): WP_Comment[]|int[]|int

函数参数

$args

数组或字符串

author_email

字符串

评论作者的邮箱地址。

author_url

字符串

评论作者的网址。

author__in

数组

要包含的多个评论作者的ID。

author__not_in

数组

要排除的多个评论作者的ID。

comment__in

数组

要包含的评论ID。

comment__not_in

数组

要排除的评论ID。

count

布尔

仅返回评论数量。

date_query

数组

日期查询参数。

fields

字符串

要返回的评论字段。

include_unapproved

数组

用户的ID或电子邮件地址数组,不论status参数的值如何,都将返回未批准的评论。

karma

整数

一些垃圾评论插件可能会用到评论字段comment_karma,为评论打分从而判断是否是垃圾评论。

meta_key

字符串

评论的元键。

meta_value

字符串

评论的元值。

meta_compare

字符串

元值比较。

meta_compare_key

字符串

元键比较。

meta_type

字符串

元值的类型。

meta_type_key

字符串

元键的类型。

meta_query

数组

元查询。

number

整数

返回评论的数量。

paged

整数

评论的当前页码。

offset

整数

评论的偏移量。

no_found_rows

布尔

是否禁用查询。

orderby

字符串或数组,默认值:comment_date_gmt

使用以下评论字段进行排序:

  • 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
  • comment__in
  • meta_value
  • meta_value_num

order

字符串

排序顺序:

  • ASC:升序;
  • DESC:降序。

parent

整数

评论的父ID。

parent__in

数组

要包含的父评论ID。

parent__not_in

数组

要排除的父评论ID。

post_author__in

数组

要包含的评论作者的ID。

post_author__not_in

数组

要排除的评论作者的ID。

post_id

整数

评论所属文章的ID。

post__not_in

数组

要排除的评论所属文章的ID。

post_author

整数

评论所属文章的作者ID。

post_status

字符串

评论所属文章的状态。

post_type

字符串

评论所属文章的类型。

post_name

字符串

评论所属文章的名称。

post_parent

整数

评论所属文章的父ID。

search

字符串

要搜索的评论内容。

status

字符串

评论状态。

type

字符串

评论类型。comment、pings、pingback、trackback

type__in

数组

要包含的评论类型。

type__not_in

数组

要排除的评论类型。

user_id

整数

评论所属用户的ID。

hierarchical

布尔

评论是否为层级评论。

  • threaded:返回树形数据;
  • flat:返回扁平数据。
  • false:忽略子项。

update_comment_meta_cache

布尔

是否为找到的评论启用元数据缓存。

update_comment_post_cache

布尔

是否为评论帖子启用缓存。

函数返回值

Array
(
    [0] => WP_Comment Object
        (
            [comment_ID] => 7
            [comment_post_ID] => 241
            [comment_author] => admin
            [comment_author_email] => beizigen@qq.com
            [comment_author_url] => 
            [comment_author_IP] => 127.0.0.1
            [comment_date] => 2024-04-08 11:57:40
            [comment_date_gmt] => 2024-04-08 03:57:40
            [comment_content] => 评论内容
            [comment_karma] => 0
            [comment_approved] => 1
            [comment_agent] => Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0
            [comment_type] => comment
            [comment_parent] => 0
            [user_id] => 1
            [children:protected] => 
            [populated_children:protected] => 
            [post_fields:protected] => Array
                (
                    [0] => post_author
                    [1] => post_date
                    [2] => post_date_gmt
                    [3] => post_content
                    [4] => post_title
                    [5] => post_excerpt
                    [6] => post_status
                    [7] => comment_status
                    [8] => ping_status
                    [9] => post_name
                    [10] => to_ping
                    [11] => pinged
                    [12] => post_modified
                    [13] => post_modified_gmt
                    [14] => post_content_filtered
                    [15] => post_parent
                    [16] => guid
                    [17] => menu_order
                    [18] => post_type
                    [19] => post_mime_type
                    [20] => comment_count
                )

        )
	......
)

函数使用示例

$args = array(
	'date_query' => array(
		'after' => '4 weeks ago',
		'before' => 'tomorrow',
		'inclusive' => true,
	),
);

$comments = get_comments( $args );
foreach ( $comments as $comment ) {
	// Output comments etc here
}

扩展阅读

get_comments()函数位于:wp-includes/comment.php

相关函数:

阿里云