Codex tools: Log in
Languages: English • 中文(简体) • (Add your language)
Contents |
Retrieve a list of comments.
<?php get_comments( $args ); ?>
<?php $defaults = array( 'author_email' => '', 'ID' => '', 'karma' => '', 'number' => '', 'offset' => '', 'orderby' => '', 'order' => 'DESC', 'parent' => '', 'post_id' => 0, 'post_author' => '', 'post_name' => '', 'post_parent' => '', 'post_status' => '', 'post_type' => '', 'status' => '', 'type' => '', 'user_id' => '', 'search' => '', 'count' => false, 'meta_key' => '', 'meta_value' => '', 'meta_query' => '', ); ?>
<?php
$comments = get_comments('post_id=15');
foreach($comments as $comment) :
echo($comment->comment_author);
endforeach;
?>
Show last 5 unapproved comments:
<?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:
<?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:
<?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:
<?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; ?>
Delete duplicate comments (same author and content):
<?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() is located in wp-includes/comment.php.