当サイト、Codex 日本語版は今後積極的な更新は行わない予定です。後継となる新ユーザーマニュアルは、https://ja.wordpress.org/support/ にあります。
万が一、当サイトで重大な問題を発見した際などは、フォーラムWordSlack #docs チャンネルでお知らせください。</p>

クラスリファレンス/WP Comment Query

提供: WordPress Codex 日本語版
< クラスリファレンス
2015年4月28日 (火) 22:14時点におけるNao (トーク | 投稿記録)による版 (2015-02-27T18:35:49‎ Dshanske 版から新規作成。)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

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

Description

WP_Comment_Query is a class, defined in wp-includes/comment.php, that allows querying WordPress database tables 'wp_comments' and 'wp_commentmeta'. The class was introduced in Version 3.1.

Usage

<?php
$args = array(
   // args here
);

// The Query
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );

// Comment Loop
if ( $comments ) {
	foreach ( $comments as $comment ) {
		echo '<p>' . $comment->comment_content . '</p>';
	}
} else {
	echo 'No comments found.';
}
?>

Default Usage

<?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' => '',
			'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
		);
?>

Methods and Properties

Properties

Methods

Parameters

$status
string) (optional) Only return comments with this status.
  • 'hold' - unapproved comments
  • 'approve' - approved comments
  • 'spam' - spam comments
  • 'trash' - trash comments
初期値: なし
$orderby
string) (optional) Set the field used to sort comments.
初期値: comment_date_gmt
$order
string) (optional) How to sort $orderby. Valid values:
  • 'ASC' - Ascending (lowest to highest).
  • 'DESC' - Descending (highest to lowest).
初期値: DESC
$number
integer) (optional) Number of comments to return. Leave blank to return all comments.
初期値: unlimited
$offset
integer) (optional) Offset from latest comment. You must include $number along with this.
初期値: 0
$post_id
integer) (optional) Only return comments for a particular post or page.
初期値: なし
$user_id
integer) (optional) Only return comments for a particular user.
初期値: なし
$count
integer) (optional) Only return the total count of comments.
初期値: なし


$type__in
array) (optional) Allows specification of specific comment types
初期値: なし
$type__not_in
array) (optional) Allows specifying all comment types except those in the array
初期値: なし
$meta_key
string) (optional) Custom meta field key.
初期値: なし
$meta_value
string) (optional) Custom meta field value.
初期値: なし
$meta_query
array) (optional) Advanced meta query parameters (available with Version 3.5).
初期値: なし
$fields
string) (optional) Which fields to return (available with Version 4.0).
  • 'ids' - Comments ID's
  • '*' - All comment fields.
初期値: *

Custom Field Parameters

Show comments associated with a certain custom field.

  • meta_key (string) - Custom field key.
  • meta_value (string) - Custom field value.
  • meta_query (array) - Custom field parameters (available with Version 3.5).
    • key (string) - Custom field key.
    • value (string|array) - Custom field value (Note: Array support is limited to a compare value of 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' or 'NOT EXISTS')
    • compare (string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS', and 'NOT EXISTS'. Default value is '='.
    • type (string) - Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'.

Display featured comments

$comment_query = new WP_Comment_Query( array( 'meta_key' => 'featured', 'meta_value' => '1' ) );

Multiple meta fields handling

$args = array(
	'meta_query' => array(
		'relation' => 'AND',
		array(
			'key' => 'featured',
			'value' => '1'
		),
		array(
			'key' => 'buried',
			'value' => '1',
			'type' => 'numeric',
			'compare' => '!='
		)
	)
 );
$comment_query = new WP_Comment_Query( $args );

Returns

(Array) 
Comment fields with the following index keys (or an empty array if there are no comments):
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)

Change Log

Source File

WP_Comment_Query is located in wp-includes/comment.php.

Resources

Related

クラスリファレンス関数リファレンスの各インデックスも参照してください。

最新英語版: WordPress Codex » Class_Reference/WP_Comment_Query最新版との差分