get_comments()获取所有的评论(可筛选)

目录

描述

Retrieve a list of comments.

get_comments() 描述

用法

<?php get_comments$args ); ?>

Default Usage

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php $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() 用法

参数

$author_email

(string) (可选) Comment author email address.

默认值: empty

$author__in

(array) (可选) Array of author IDs to include comments for.

默认值: empty

$author__not_in

(array) (可选) Array of author IDs to exclude comments for.

默认值: empty

$comment__in

(array) (可选) Array of comment IDs to include.

默认值: empty

$comment__not_in

(array) (可选) Array of comment IDs to exclude.

默认值: empty

$count

(bool) (可选) Whether to return a comment count (true) or array of comment objects (false).

默认值: false

$date_query

(array) (可选) Date query clauses to limit comments by. See WP_Date_Query.

默认值: null

$fields

(string) (可选) Comment fields to return. Accepts 'ids' for comment IDs only or empty for all fields.

默认值: empty

$ID

(int) (可选) Currently unused.

默认值: None

$include_unapproved

(array) (可选) Array of IDs or email addresses of users whose unapproved comments will be returned by the query regardless of $status.

默认值: empty

$karma

(int) (可选) Karma score to retrieve matching comments for.

默认值: empty

$meta_key

(string) (可选) Include comments with a matching comment meta key.

默认值: empty

$meta_value

(string) (可选) Include comments with a matching comment meta value. Requires $meta_key to be set.

默认值: empty

$meta_query

(array) (可选) Meta query clauses to limit retrieved comments by. See WP_Meta_Query.

默认值: empty

$number

(int) (可选) Maximum number of comments to retrieve.

默认值: null (no limit)

$offset

(int) (0) Number of comments to offset the query. Used to build LIMIT clause.

默认值: None

$orderby

(string_array) (可选) Comment status or array of statuses. To use 'meta_value' or 'meta_value_num', $meta_key must also be defined. To sort by a specific $meta_query clause, use that clause's array key.

  • 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

(string) (可选) How to order retrieved comments. Accepts

  • ASC
  • DESC
  • .

    默认值: 'DESC'.

$parent

(int) (可选) Parent ID of comment to retrieve children of.

默认值: empty

$post_author__in

(array) (可选) Array of author IDs to retrieve comments for.

默认值: empty

$post_author__not_in

(array) (可选) Array of author IDs *not* to retrieve comments for.

默认值: empty

$post_ID

(int) (可选) Currently unused

默认值: None

$post_id

(int) (可选) Limit results to those affiliated with a given post ID.

默认值: 0

$post__in

(array) (可选) Array of post IDs to include affiliated comments for.

默认值: empty

$post__not_in

(array) (可选) Array of post IDs to exclude affiliated comments for.

默认值: empty

$post_author

(int) (可选) Comment author ID to limit results by.

默认值: empty

$post_status

(string) (可选) Post status to retrieve affiliated comments for.

默认值: empty

$post_type

(string) (可选) Post type to retrieve affiliated comments for.

默认值: empty

$post_name

(string) (可选) Post name to retrieve affiliated comments for.

默认值: empty

$post_parent

(int) (可选) Post parent ID to retrieve affiliated comments for.

默认值: empty

$search

(string) (可选) Search term(s) to retrieve matching comments for.

默认值: empty

$status

(string) ('all') optional

默认值: None

$type

(string_array) (可选) Include comments of a given type, or array of types. Accepts 'comment', 'pings' (includes 'pingback' and 'trackback'), or any custom type string.

默认值: empty

$type__in

(array) (可选) Include comments from a given array of comment types.

默认值: empty

$type__not_in

(array) (可选) Exclude comments from a given array of comment types.

默认值: empty

$user_id

(int) (可选) Include comments for a specific user ID.

默认值: empty

get_comments() 参数

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php 
$comments = get_comments('post_id=15');
foreach($comments as $comment) :
	echo($comment->comment_author);
endforeach;
?>

Show last 5 unapproved comments:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php
$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;
?>

Show comment counts of a post:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php
$args = array(
	'post_id' => 1, // use post_id, not post_ID
        'count' => true //return only the count
);
$comments = get_comments($args);
echo $comments

?>

Show comment counts of a user:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php
$args = array(
	'user_id' => 1, // use user_id
        'count' => true //return only the count
);
$comments = get_comments($args);
echo $comments

?>

Show comments of a user:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php
$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;

?>

Get comments from last 4 weeks:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

         $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
        }

Delete duplicate comments (same author and content):

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php

// 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;

?>

get_comments() 示例

源文件

get_comments() 函数的代码位于 wp-includes/comment.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve a list of comments.
 *
 * The comment list can be for the blog as a whole or for an individual post.
 *
 * @since 2.7.0
 *
 * @param string|array $args Optional. Array or string of arguments. See {@see WP_Comment_Query::parse_query()}
 *                           for information on accepted arguments. Default empty.
 * @return int|array List of comments or number of found comments if `$count` argument is true.
 */
function get_comments( $args = '' ) {
	$query = new WP_Comment_Query;
	return $query->query( $args );
}

get_comments() 源文件

相关