• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

「関数リファレンス/get comments」の版間の差分

提供: WordPress Codex 日本語版
移動先: 案内検索
(en:Function Reference/get_comments 17:07, 16 September 2016 Bcole808 版を流し込み。)
 
(https://developer.wordpress.org/reference/functions/get_comments/ をマージ。)
3行目: 3行目:
 
== 説明<span id="Description"></span> ==
 
== 説明<span id="Description"></span> ==
  
Retrieve a list of comments.
+
コメントのリストを取得します。
 +
 
 +
ブログ全体から、または個別投稿からコメントのリストを取得できます。
  
 
== 使い方<span id="Usage"></span> ==
 
== 使い方<span id="Usage"></span> ==
9行目: 11行目:
 
  <?php get_comments( $args ); ?>
 
  <?php get_comments( $args ); ?>
  
=== デフォルトの使い方<span id="Default_Usage"></span> ===
+
== パラメータ<span id="Parameters"></span> ==
  
<pre>
+
{{Parameter|$args|文字列/配列|引数の配列または文字列。有効な引数の情報は [https://developer.wordpress.org/reference/classes/wp_comment_query/parse_query/ WP_Comment_Query::parse_query()] を見てください。|オプション|<tt>&apos;&apos;</tt>}}
<?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 ); ?>
+
</pre>
+
  
== パラメータ<span id="Parameters"></span> ==
+
=== 引数 ===
  
{{Parameter|$author_email|string|Comment author email address.|optional|empty}}
+
{{Parameter|author_email|string|Comment author email address.|optional|empty}}
{{Parameter|$author__in|array|Array of author IDs to include comments for.|optional|empty}}
+
{{Parameter|author__in|array|Array of author IDs to include comments for.|optional|empty}}
{{Parameter|$author__not_in|array|Array of author IDs to exclude comments for.|optional|empty}}
+
{{Parameter|author__not_in|array|Array of author IDs to exclude comments for.|optional|empty}}
{{Parameter|$comment__in|array|Array of comment IDs to include.|optional|empty}}
+
{{Parameter|comment__in|array|Array of comment IDs to include.|optional|empty}}
{{Parameter|$comment__not_in|array|Array of comment IDs to exclude.|optional|empty}}
+
{{Parameter|comment__not_in|array|Array of comment IDs to exclude.|optional|empty}}
{{Parameter|$count|bool|Whether to return a comment count (true) or array of comment objects (false).|optional|false}}
+
{{Parameter|count|bool|Whether to return a comment count (true) or array of comment objects (false).|optional|false}}
{{Parameter|$date_query|array|Date query clauses to limit comments by. See WP_Date_Query.|optional|null}}
+
{{Parameter|date_query|array|Date query clauses to limit comments by. See WP_Date_Query.|optional|null}}
{{Parameter|$fields|string|Comment fields to return. Accepts 'ids' for comment IDs only or empty for all fields.|optional|empty}}
+
{{Parameter|fields|string|Comment fields to return. Accepts 'ids' for comment IDs only or empty for all fields.|optional|empty}}
{{Parameter|$ID|int|'''Currently unused.'''|optional}}
+
{{Parameter|ID|int|'''Currently unused.'''|optional}}
{{Parameter|$include_unapproved|array|Array of IDs or email addresses of users whose unapproved comments will be returned by the query regardless of ''$status''.|optional|empty}}
+
{{Parameter|include_unapproved|array|Array of IDs or email addresses of users whose unapproved comments will be returned by the query regardless of ''status''.|optional|empty}}
{{Parameter|$karma|int|Karma score to retrieve matching comments for.|optional|empty}}
+
{{Parameter|karma|int|Karma score to retrieve matching comments for.|optional|empty}}
{{Parameter|$meta_key|string|Include comments with a matching comment meta key.|optional|empty}}
+
{{Parameter|meta_key|string|Include comments with a matching comment meta key.|optional|empty}}
{{Parameter|$meta_value|string|Include comments with a matching comment meta value. Requires ''$meta_key'' to be set.|optional|empty}}
+
{{Parameter|meta_value|string|Include comments with a matching comment meta value. Requires ''meta_key'' to be set.|optional|empty}}
{{Parameter|$meta_query|array|Meta query clauses to limit retrieved comments by. See WP_Meta_Query.|optional|empty}}
+
{{Parameter|meta_query|array|Meta query clauses to limit retrieved comments by. See WP_Meta_Query.|optional|empty}}
{{Parameter|$number|int|Maximum number of comments to retrieve.|optional|null (no limit)}}
+
{{Parameter|number|int|Maximum number of comments to retrieve.|optional|null (no limit)}}
{{Parameter|$offset|int|Number of comments to offset the query. Used to build LIMIT clause.|0}}
+
{{Parameter|offset|int|Number of comments to offset the query. Used to build LIMIT clause.|0}}
{{Parameter|$orderby|string_array|Comment field or array of comment fields. 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. <ul><li>''comment_agent''</li><li>''comment_approved''</li><li>''comment_author''</li><li>''comment_author_email''</li><li>''comment_author_IP''</li><li>''comment_author_url''</li><li>''comment_content''</li><li>''comment_date''</li><li>''comment_date_gmt''</li><li>''comment_ID''</li><li>''comment_karma''</li><li>''comment_parent''</li><li>''comment_post_ID''</li><li>''comment_type''</li><li>''user_id''</li><li>''meta_value''</li><li>''meta_value_num''</li><li>the value of $meta_key</li><li>and the array keys of ''$meta_query''</li><li>Also accepts false, an empty array, or 'none' to disable ''ORDER BY'' clause</li>|optional|''comment_date_gmt''}}
+
{{Parameter|orderby|string_array|Comment field or array of comment fields. 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. <ul><li>''comment_agent''</li><li>''comment_approved''</li><li>''comment_author''</li><li>''comment_author_email''</li><li>''comment_author_IP''</li><li>''comment_author_url''</li><li>''comment_content''</li><li>''comment_date''</li><li>''comment_date_gmt''</li><li>''comment_ID''</li><li>''comment_karma''</li><li>''comment_parent''</li><li>''comment_post_ID''</li><li>''comment_type''</li><li>''user_id''</li><li>''meta_value''</li><li>''meta_value_num''</li><li>the value of meta_key</li><li>and the array keys of ''meta_query''</li><li>Also accepts false, an empty array, or 'none' to disable ''ORDER BY'' clause</li>|optional|''comment_date_gmt''}}
{{Parameter|$order|string|How to order retrieved comments. Accepts <ul><li>''ASC''</li><li>''DESC''</li>.|optional|'DESC'.}}
+
{{Parameter|order|string|How to order retrieved comments. Accepts <ul><li>''ASC''</li><li>''DESC''</li>.|optional|'DESC'.}}
{{Parameter|$parent|int|Parent ID of comment to retrieve children of.|optional|empty}}
+
{{Parameter|parent|int|Parent ID of comment to retrieve children of.|optional|empty}}
{{Parameter|$post_author__in|array|Array of author IDs to retrieve comments for.|optional|empty}}
+
{{Parameter|post_author__in|array|Array of author IDs to retrieve comments for.|optional|empty}}
{{Parameter|$post_author__not_in|array|Array of author IDs *not* to retrieve comments for.|optional|empty}}
+
{{Parameter|post_author__not_in|array|Array of author IDs *not* to retrieve comments for.|optional|empty}}
{{Parameter|$post_ID|int|'''Currently unused'''|optional}}
+
{{Parameter|post_ID|int|'''Currently unused'''|optional}}
{{Parameter|$post_id|int|Limit results to those affiliated with a given post ID.|optional|0}}
+
{{Parameter|post_id|int|Limit results to those affiliated with a given post ID.|optional|0}}
{{Parameter|$post__in|array|Array of post IDs to include affiliated comments for.|optional|empty}}
+
{{Parameter|post__in|array|Array of post IDs to include affiliated comments for.|optional|empty}}
{{Parameter|$post__not_in|array|Array of post IDs to exclude affiliated comments for.|optional|empty}}
+
{{Parameter|post__not_in|array|Array of post IDs to exclude affiliated comments for.|optional|empty}}
{{Parameter|$post_author|int|Comment author ID to limit results by.|optional|empty}}
+
{{Parameter|post_author|int|Comment author ID to limit results by.|optional|empty}}
{{Parameter|$post_status|string|Post status to retrieve affiliated comments for.|optional|empty}}
+
{{Parameter|post_status|string|Post status to retrieve affiliated comments for.|optional|empty}}
{{Parameter|$post_type|string|Post type to retrieve affiliated comments for.|optional|empty}}
+
{{Parameter|post_type|string|Post type to retrieve affiliated comments for.|optional|empty}}
{{Parameter|$post_name|string|Post name to retrieve affiliated comments for.|optional|empty}}
+
{{Parameter|post_name|string|Post name to retrieve affiliated comments for.|optional|empty}}
{{Parameter|$post_parent|int|Post parent ID to retrieve affiliated comments for.|optional|empty}}
+
{{Parameter|post_parent|int|Post parent ID to retrieve affiliated comments for.|optional|empty}}
{{Parameter|$search|string|Search term(s) to retrieve matching comments for.|optional|empty}}
+
{{Parameter|search|string|Search term(s) to retrieve matching comments for.|optional|empty}}
{{Parameter|$status|string|Comment status to limit results by. Accepts 'hold', 'approve', 'all', or a custom comment status.|optional|''all''}}
+
{{Parameter|status|string|Comment status to limit results by. Accepts 'hold', 'approve', 'all', or a custom comment status.|optional|''all''}}
{{Parameter|$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.|optional|empty}}
+
{{Parameter|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.|optional|empty}}
{{Parameter|$type__in|array|Include comments from a given array of comment types.|optional|empty}}
+
{{Parameter|type__in|array|Include comments from a given array of comment types.|optional|empty}}
{{Parameter|$type__not_in|array|Exclude comments from a given array of comment types.|optional|empty}}
+
{{Parameter|type__not_in|array|Exclude comments from a given array of comment types.|optional|empty}}
{{Parameter|$user_id|int|Include comments for a specific user ID.|optional|empty}}
+
{{Parameter|user_id|int|Include comments for a specific user ID.|optional|empty}}
  
 
== 戻り値<span id="Returns"></span> ==
 
== 戻り値<span id="Returns"></span> ==
  
; (Object) : Comment fields with the following properties (or an empty array if there are no comments):
+
{{Return||整数/配列|コメントのリスト(配列)。<tt>'count'</tt> 引数が true なら見つかったコメントの数。なお、各コメントは [https://developer.wordpress.org/reference/classes/wp_comment/ WP_Comment] オブジェクトです。}}
 +
 
 +
=== WP_Comment オブジェクトの内容 ===
  
 
; <tt>comment_ID</tt> : (''integer'') The comment ID
 
; <tt>comment_ID</tt> : (''integer'') The comment ID
114行目: 78行目:
 
== 用例<span id="Examples"></span> ==
 
== 用例<span id="Examples"></span> ==
  
 +
コメントの作成者を表示します:
 
<pre><?php  
 
<pre><?php  
$comments = get_comments('post_id=15');
+
$comments = get_comments( array( 'post_id' => 15 ) );
foreach($comments as $comment) :
+
echo($comment->comment_author);
+
foreach ( $comments as $comment ) :
 +
    echo $comment->comment_author;
 
endforeach;
 
endforeach;
 
?></pre>
 
?></pre>
  
Show last 5 unapproved comments:
+
未承認のコメントを最新から5つ表示します:
 
<pre><?php
 
<pre><?php
 
$args = array(
 
$args = array(
'status' => 'hold',
+
    'status' => 'hold',
'number' => '5',
+
    'number' => '5',
'post_id' => 1, // use post_id, not post_ID
+
    'post_id' => 1, // post_ID ではなく post_id を使う
 
);
 
);
$comments = get_comments($args);
+
$comments = get_comments( $args );
foreach($comments as $comment) :
+
echo($comment->comment_author . '<br />' . $comment->comment_content);
+
foreach ( $comments as $comment ) :
 +
    echo $comment->comment_author . '<br />' . $comment->comment_content;
 
endforeach;
 
endforeach;
 
?></pre>
 
?></pre>
  
Show comment counts of a post:
+
ある投稿のコメントの数を表示します:
 
<pre><?php
 
<pre><?php
 
$args = array(
 
$args = array(
'post_id' => 1, // use post_id, not post_ID
+
    'post_id' => 1,   // post_ID ではなく post_id を使う
        'count' => true //return only the count
+
    'count'   => true // 個数だけを返す
 
);
 
);
$comments = get_comments($args);
+
$comments_count = get_comments( $args );
echo $comments
+
echo $comments_count;
 
+
 
?></pre>
 
?></pre>
  
 
+
あるユーザーのコメントの数を表示します:
Show comment counts of a user:
+
 
<pre><?php
 
<pre><?php
 
$args = array(
 
$args = array(
'user_id' => 1, // use user_id
+
    'user_id' => 1,   // user_id を使う
        'count' => true //return only the count
+
    'count'   => true // 個数だけを返す
 
);
 
);
$comments = get_comments($args);
+
$comments_count = get_comments( $args );
echo $comments
+
 
+
 
?></pre>
 
?></pre>
  
Show comments of a user:
+
あるユーザーのコメントを表示します:
 
<pre><?php
 
<pre><?php
 
$args = array(
 
$args = array(
'user_id' => 1, // use user_id
+
    'user_id' => 1, // user_id を使う
 
+
 
);
 
);
$comments = get_comments($args);
+
$comments = get_comments( $args );
foreach($comments as $comment) :
+
echo($comment->comment_author . '<br />' . $comment->comment_content);
+
foreach ( $comments as $comment ) :
 +
    echo $comment->comment_author . '<br />' . $comment->comment_content;
 
endforeach;
 
endforeach;
 
 
?></pre>
 
?></pre>
  
Get comments from last 4 weeks:
+
最近4週間のコメントを取得します:
<pre>
+
<pre><?php
        $args = array(
+
$args = array(
                        'date_query' => array(
+
    'date_query' => array(
                                                        'after' => '4 week ago',
+
        'after' => '4 weeks ago', // 4週前より後
                                                        'before' => 'tomorrow',
+
        'before' => 'tomorrow',   // 明日より前
                                                        'inclusive' => true,
+
        'inclusive' => true,     // 「ぴったり」を含める
                                        ),  
+
    ),
                        );
+
);
        $posts = get_comments($args);
+
 
+
$comments = get_comments( $args );
        foreach ($posts as $post) {
+
foreach ( $comments as $comment ) {
                // Output comments etc here
+
    // ここでコメントなどを出力
        }
+
}
</pre>
+
?></pre>
  
Delete duplicate comments (same author and content):
+
重複したコメント(作成者と内容がどちらも同じ)を削除します:
 
<pre><?php
 
<pre><?php
  
// get all approved comments with empty number arg
+
// 個数の指定を空文字列にして承認済みコメントを全て取得
$all_comments=get_comments( array('status' => 'approve', 'number'=>'') );
+
$all_comments=get_comments( array( 'status' => 'approve', 'number' => '' ) );
  
// array to hold comment ids that are dupes
+
// 重複したコメントの id を保持する配列
$comment_ids_to_delete=array();
+
$comment_ids_to_delete = array();
  
foreach($all_comments as $k=>$c)
+
foreach ( $all_comments as $k=>$c )
 
{
 
{
$kk=($k-1); // the previous comments index in all_comments array
+
$kk = ( $k - 1 ); // 配列 all_comment 内にある直前のコメントのインデックス
$pc=$all_comments[$kk]; // the previous comment object
+
$pc = $all_comments[$kk]; // 直前のコメントのオブジェクト
  
// 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) {
+
if ( $pc->comment_author == $c->comment_author && $pc->comment_content == $c->comment_content ) {
$comment_ids_to_delete[]=$pc->comment_ID; // previous comment id
+
$comment_ids_to_delete[] = $pc->comment_ID; // 直前のコメントの id
 
}
 
}
 
}
 
}
  
// delete the comment by id
+
// コメントの id を指定して削除
foreach($comment_ids_to_delete as $k=>$v):
+
foreach ( $comment_ids_to_delete as $k=>$v ) :
 
wp_delete_comment($v);
 
wp_delete_comment($v);
 
endforeach;
 
endforeach;

2017年5月28日 (日) 16:58時点における版

このページ「関数リファレンス/get comments」は未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

説明

コメントのリストを取得します。

ブログ全体から、または個別投稿からコメントのリストを取得できます。

使い方

<?php get_comments( $args ); ?>

パラメータ

$args
文字列/配列) (オプション) 引数の配列または文字列。有効な引数の情報は WP_Comment_Query::parse_query() を見てください。
初期値: ''

引数

author_email
string) (optional) Comment author email address.
初期値: empty
author__in
array) (optional) Array of author IDs to include comments for.
初期値: empty
author__not_in
array) (optional) Array of author IDs to exclude comments for.
初期値: empty
comment__in
array) (optional) Array of comment IDs to include.
初期値: empty
comment__not_in
array) (optional) Array of comment IDs to exclude.
初期値: empty
count
bool) (optional) Whether to return a comment count (true) or array of comment objects (false).
初期値: false
date_query
array) (optional) Date query clauses to limit comments by. See WP_Date_Query.
初期値: null
fields
string) (optional) Comment fields to return. Accepts 'ids' for comment IDs only or empty for all fields.
初期値: empty
ID
int) (optionalCurrently unused.
初期値: なし
include_unapproved
array) (optional) Array of IDs or email addresses of users whose unapproved comments will be returned by the query regardless of status.
初期値: empty
karma
int) (optional) Karma score to retrieve matching comments for.
初期値: empty
meta_key
string) (optional) Include comments with a matching comment meta key.
初期値: empty
meta_value
string) (optional) Include comments with a matching comment meta value. Requires meta_key to be set.
初期値: empty
meta_query
array) (optional) Meta query clauses to limit retrieved comments by. See WP_Meta_Query.
初期値: empty
number
int) (optional) Maximum number of comments to retrieve.
初期値: null (no limit)
offset
int) (0) Number of comments to offset the query. Used to build LIMIT clause.
初期値: なし
orderby
string_array) (optional) Comment field or array of comment fields. 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) (optional) How to order retrieved comments. Accepts
  • ASC
  • DESC
  • .
    初期値: 'DESC'.
parent
int) (optional) Parent ID of comment to retrieve children of.
初期値: empty
post_author__in
array) (optional) Array of author IDs to retrieve comments for.
初期値: empty
post_author__not_in
array) (optional) Array of author IDs *not* to retrieve comments for.
初期値: empty
post_ID
int) (optionalCurrently unused
初期値: なし
post_id
int) (optional) Limit results to those affiliated with a given post ID.
初期値: 0
post__in
array) (optional) Array of post IDs to include affiliated comments for.
初期値: empty
post__not_in
array) (optional) Array of post IDs to exclude affiliated comments for.
初期値: empty
post_author
int) (optional) Comment author ID to limit results by.
初期値: empty
post_status
string) (optional) Post status to retrieve affiliated comments for.
初期値: empty
post_type
string) (optional) Post type to retrieve affiliated comments for.
初期値: empty
post_name
string) (optional) Post name to retrieve affiliated comments for.
初期値: empty
post_parent
int) (optional) Post parent ID to retrieve affiliated comments for.
初期値: empty
search
string) (optional) Search term(s) to retrieve matching comments for.
初期値: empty
status
string) (optional) Comment status to limit results by. Accepts 'hold', 'approve', 'all', or a custom comment status.
初期値: all
type
string_array) (optional) 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) (optional) Include comments from a given array of comment types.
初期値: empty
type__not_in
array) (optional) Exclude comments from a given array of comment types.
初期値: empty
user_id
int) (optional) Include comments for a specific user ID.
初期値: empty

戻り値

(整数/配列) 
コメントのリスト(配列)。'count' 引数が true なら見つかったコメントの数。なお、各コメントは WP_Comment オブジェクトです。

WP_Comment オブジェクトの内容

comment_ID 
(integer) The comment ID
comment_post_ID 
(integer) The ID of the post/page that this comment responds to
comment_author 
(string) The comment author's name
comment_author_email 
(string) The comment author's email
comment_author_url 
(string) The comment author's webpage
comment_author_IP 
(string) The comment author's IP
comment_date 
(string) The datetime of the comment (YYYY-MM-DD HH:MM:SS)
comment_date_gmt 
(string) The GMT datetime of the comment (YYYY-MM-DD HH:MM:SS)
comment_content 
(string) The comment's content
comment_karma 
(integer) The comment's karma
comment_approved 
(string) The comment approval level (0, 1 or "spam")
comment_agent 
(string) The commenter's user agent (browser, operating system, etc.)
comment_type 
(string) The comment's type if meaningfull (pingback|trackback), empty for normal comments
comment_parent 
(string) The parent comment's ID for nested comments (0 for top level)
user_id 
(integer) The comment author's ID if s/he is registered (0 otherwise)

用例

コメントの作成者を表示します:

<?php 
$comments = get_comments( array( 'post_id' => 15 ) );
 
foreach ( $comments as $comment ) :
    echo $comment->comment_author;
endforeach;
?>

未承認のコメントを最新から5つ表示します:

<?php
$args = array(
    'status'  => 'hold',
    'number'  => '5',
    'post_id' => 1, // post_ID ではなく post_id を使う
);
$comments = get_comments( $args );
 
foreach ( $comments as $comment ) :
    echo $comment->comment_author . '<br />' . $comment->comment_content;
endforeach;
?>

ある投稿のコメントの数を表示します:

<?php
$args = array(
    'post_id' => 1,   // post_ID ではなく post_id を使う
    'count'   => true // 個数だけを返す
);
$comments_count = get_comments( $args );
echo $comments_count;
?>

あるユーザーのコメントの数を表示します:

<?php
$args = array(
    'user_id' => 1,   // user_id を使う
    'count'   => true // 個数だけを返す
);
$comments_count = get_comments( $args );
?>

あるユーザーのコメントを表示します:

<?php
$args = array(
    'user_id' => 1, // user_id を使う
);
$comments = get_comments( $args );
 
foreach ( $comments as $comment ) :
    echo $comment->comment_author . '<br />' . $comment->comment_content;
endforeach;
?>

最近4週間のコメントを取得します:

<?php
$args = array(
    'date_query' => array(
        'after' => '4 weeks ago', // 4週前より後
        'before' => 'tomorrow',   // 明日より前
        'inclusive' => true,      // 「ぴったり」を含める
    ),
);
 
$comments = get_comments( $args );
foreach ( $comments as $comment ) {
    // ここでコメントなどを出力
}
?>

重複したコメント(作成者と内容がどちらも同じ)を削除します:

<?php

// 個数の指定を空文字列にして承認済みコメントを全て取得
$all_comments=get_comments( array( 'status' => 'approve', 'number' => '' ) );

// 重複したコメントの id を保持する配列
$comment_ids_to_delete = array();

foreach ( $all_comments as $k=>$c )
{
	$kk = ( $k - 1 ); // 配列 all_comment 内にある直前のコメントのインデックス
	$pc = $all_comments[$kk]; // 直前のコメントのオブジェクト

	// コメントの作成者が同じで、さらに内容も同じなら、削除対象として配列へ追加
	if ( $pc->comment_author == $c->comment_author && $pc->comment_content == $c->comment_content ) {
		$comment_ids_to_delete[] = $pc->comment_ID; // 直前のコメントの id
	}
}

// コメントの id を指定して削除
foreach ( $comment_ids_to_delete as $k=>$v ) :
	wp_delete_comment($v);
endforeach;

?>

ソースファイル

get_comments() is located in wp-includes/comment.php.

関連項目


関数リファレンステンプレートタグ目次もご覧ください。


最新英語版: WordPress Codex » Function Reference/get_comments最新版との差分