WordPress获取已批准评论函数:get_approved_comments

WordPress函数get_approved_comments用于获取已批准的评论或已驳回的评论。

get_approved_comments( int $post_id, array $args = array() ): WP_Comment[]|int[]|int

函数参数

$post_id

整数

文章ID

$args

数组,可用值如下:

status

整数

评论状态,默认为已批准的评论。

  • 1:已批准的评论;
  • 0:已驳回的评论;

post_id

整数

文章ID

order

字符串

排序方式,默认为升序:

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

函数返回值

get_approved_comments()函数返回一个包含评论对象的数组。

Array
(
    [0] => WP_Comment Object
        (
            [comment_ID] => 2
            [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:16
            [comment_date_gmt] => 2024-04-08 03:57:16
            [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
                )

        )
	......
)

函数使用示例

<?php
   $postID = 241;
   $comment_array = get_approved_comments($postID);
   foreach($comment_array as $comment){
      echo $comment->comment_ID . " => " . $comment->comment_post_ID . "\n";
   }
?>

扩展阅读

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

相关函数:

阿里云