* Comment API: WP_Comment_Query class
* Core class used for querying comments.
* @see WP_Comment_Query::__construct() for accepted arguments.
#[AllowDynamicProperties]
* SQL for database query.
* Metadata query container
* @var WP_Meta_Query A meta query instance.
public $meta_query = false;
* Metadata query clauses.
protected $meta_query_clauses;
protected $sql_clauses = array(
* Stored after the {@see 'comments_clauses'} filter is run on the compiled WHERE sub-clauses.
protected $filtered_where_clause;
* @var WP_Date_Query A date query instance.
public $date_query = false;
* Query vars set by the user.
* Default values for query vars.
public $query_var_defaults;
* List of comments located by the query.
* @var int[]|WP_Comment[]
* The amount of found comments for the current query.
public $found_comments = 0;
public $max_num_pages = 0;
* Make private/protected methods readable for backward compatibility.
* @param string $name Method to call.
* @param array $arguments Arguments to pass when calling.
* @return mixed|false Return value of the callback, false otherwise.
public function __call( $name, $arguments ) {
if ( 'get_search_sql' === $name ) {
return $this->get_search_sql( ...$arguments );
* Sets up the comment query, based on the query vars passed.
* @since 4.4.0 `$parent__in` and `$parent__not_in` were added.
* @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache`, `$no_found_rows`,
* `$hierarchical`, and `$update_comment_post_cache` were added.
* @since 4.5.0 Introduced the `$author_url` argument.
* @since 4.6.0 Introduced the `$cache_domain` argument.
* @since 4.9.0 Introduced the `$paged` argument.
* @since 5.1.0 Introduced the `$meta_compare_key` argument.
* @since 5.3.0 Introduced the `$meta_type_key` argument.
* @param string|array $query {
* Optional. Array or query string of comment query parameters. Default empty.
* @type string $author_email Comment author email address. Default empty.
* @type string $author_url Comment author URL. Default empty.
* @type int[] $author__in Array of author IDs to include comments for. Default empty.
* @type int[] $author__not_in Array of author IDs to exclude comments for. Default empty.
* @type int[] $comment__in Array of comment IDs to include. Default empty.
* @type int[] $comment__not_in Array of comment IDs to exclude. Default empty.
* @type bool $count Whether to return a comment count (true) or array of
* comment objects (false). Default false.
* @type array $date_query Date query clauses to limit comments by. See WP_Date_Query.
* @type string $fields Comment fields to return. Accepts 'ids' for comment IDs
* only or empty for all fields. Default empty.
* @type array $include_unapproved Array of IDs or email addresses of users whose unapproved
* comments will be returned by the query regardless of
* `$status`. Default empty.
* @type int $karma Karma score to retrieve matching comments for.
* @type string|string[] $meta_key Meta key or keys to filter by.
* @type string|string[] $meta_value Meta value or values to filter by.
* @type string $meta_compare MySQL operator used for comparing the meta value.
* See WP_Meta_Query::__construct() for accepted values and default value.
* @type string $meta_compare_key MySQL operator used for comparing the meta key.
* See WP_Meta_Query::__construct() for accepted values and default value.
* @type string $meta_type MySQL data type that the meta_value column will be CAST to for comparisons.
* See WP_Meta_Query::__construct() for accepted values and default value.
* @type string $meta_type_key MySQL data type that the meta_key column will be CAST to for comparisons.
* See WP_Meta_Query::__construct() for accepted values and default value.
* @type array $meta_query An associative array of WP_Meta_Query arguments.
* See WP_Meta_Query::__construct() for accepted values.
* @type int $number Maximum number of comments to retrieve.
* Default empty (no limit).
* @type int $paged When used with `$number`, defines the page of results to return.
* When used with `$offset`, `$offset` takes precedence. Default 1.
* @type int $offset Number of comments to offset the query. Used to build
* LIMIT clause. Default 0.
* @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query.
* @type string|array $orderby 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. Accepts:
* - 'comment_author_email'
* - The value of `$meta_key`
* - The array keys of `$meta_query`
* - false, an empty array, or 'none' to disable `ORDER BY` clause.
* Default: 'comment_date_gmt'.
* @type string $order How to order retrieved comments. Accepts 'ASC', 'DESC'.
* @type int $parent Parent ID of comment to retrieve children of.
* @type int[] $parent__in Array of parent IDs of comments to retrieve children for.
* @type int[] $parent__not_in Array of parent IDs of comments *not* to retrieve
* children for. Default empty.
* @type int[] $post_author__in Array of author IDs to retrieve comments for.
* @type int[] $post_author__not_in Array of author IDs *not* to retrieve comments for.
* @type int $post_id Limit results to those affiliated with a given post ID.
* @type int[] $post__in Array of post IDs to include affiliated comments for.
* @type int[] $post__not_in Array of post IDs to exclude affiliated comments for.
* @type int $post_author Post author ID to limit results by. Default empty.
* @type string|string[] $post_status Post status or array of post statuses to retrieve
* affiliated comments for. Pass 'any' to match any value.
* @type string|string[] $post_type Post type or array of post types to retrieve affiliated
* comments for. Pass 'any' to match any value. Default empty.
* @type string $post_name Post name to retrieve affiliated comments for.
* @type int $post_parent Post parent ID to retrieve affiliated comments for.
* @type string $search Search term(s) to retrieve matching comments for.
* @type string|array $status Comment statuses to limit results by. Accepts an array
* or space/comma-separated list of 'hold' (`comment_status=0`),
* 'approve' (`comment_status=1`), 'all', or a custom
* comment status. Default 'all'.
* @type string|string[] $type Include comments of a given type, or array of types.
* Accepts 'comment', 'pings' (includes 'pingback' and
* 'trackback'), or any custom type string. Default empty.
* @type string[] $type__in Include comments from a given array of comment types.
* @type string[] $type__not_in Exclude comments from a given array of comment types.
* @type int $user_id Include comments for a specific user ID. Default empty.
* @type bool|string $hierarchical Whether to include comment descendants in the results.
* - 'threaded' returns a tree, with each comment's children
* stored in a `children` property on the `WP_Comment` object.
* - 'flat' returns a flat array of found comments plus
* - Boolean `false` leaves out descendants.
* The parameter is ignored (forced to `false`) when
* `$fields` is 'ids' or 'counts'. Accepts 'threaded',
* 'flat', or false. Default: false.
* @type string $cache_domain Unique cache key to be produced when this query is stored in
* an object cache. Default is 'core'.
* @type bool $update_comment_meta_cache Whether to prime the metadata cache for found comments.
* @type bool $update_comment_post_cache Whether to prime the cache for comment posts.
public function __construct( $query = '' ) {
$this->query_var_defaults = array(
'include_unapproved' => '',
'post_author__not_in' => '',
'date_query' => null, // See WP_Date_Query.
'cache_domain' => 'core',
'update_comment_meta_cache' => true,
'update_comment_post_cache' => false,
if ( ! empty( $query ) ) {
* Parse arguments passed to the comment query with default query parameters.
* @since 4.2.0 Extracted from WP_Comment_Query::query().
* @param string|array $query WP_Comment_Query arguments. See WP_Comment_Query::__construct() for accepted arguments.
public function parse_query( $query = '' ) {
$query = $this->query_vars;
$this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
* Fires after the comment query vars have been parsed.
* @param WP_Comment_Query $query The WP_Comment_Query instance (passed by reference).
do_action_ref_array( 'parse_comment_query', array( &$this ) );
* Sets up the WordPress query for retrieving comments.
* @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in',
* 'post_author__not_in', 'author__in', 'author__not_in', 'post__in',
* 'post__not_in', 'include_unapproved', 'type__in', and 'type__not_in'
* arguments to $query_vars.
* @since 4.2.0 Moved parsing to WP_Comment_Query::parse_query().
* @param string|array $query Array or URL query string of parameters.
* @return array|int List of comments, or number of comments when 'count' is passed as a query var.
public function query( $query ) {
$this->query_vars = wp_parse_args( $query );
return $this->get_comments();
* Get a list of comments matching the query vars.
* @global wpdb $wpdb WordPress database abstraction object.
* @return int|int[]|WP_Comment[] List of comments or number of found comments if `$count` argument is true.
public function get_comments() {
$this->meta_query = new WP_Meta_Query();
$this->meta_query->parse_query_vars( $this->query_vars );
* Fires before comments are retrieved.
* @param WP_Comment_Query $query Current instance of WP_Comment_Query (passed by reference).
do_action_ref_array( 'pre_get_comments', array( &$this ) );
// Reparse query vars, in case they were modified in a 'pre_get_comments' callback.
$this->meta_query->parse_query_vars( $this->query_vars );
if ( ! empty( $this->meta_query->queries ) ) {
$this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
* Filters the comments data before the query takes place.
* Return a non-null value to bypass WordPress' default comment queries.
* The expected return type from this filter depends on the value passed
* in the request query vars:
* - When `$this->query_vars['count']` is set, the filter should return
* the comment count as an integer.
* - When `'ids' === $this->query_vars['fields']`, the filter should return
* an array of comment IDs.
* - Otherwise the filter should return an array of WP_Comment objects.
* Note that if the filter returns an array of comment data, it will be assigned
* to the `comments` property of the current WP_Comment_Query instance.
* Filtering functions that require pagination information are encouraged to set
* the `found_comments` and `max_num_pages` properties of the WP_Comment_Query object,
* passed to the filter by reference. If WP_Comment_Query does not perform a database
* query, it will not have enough information to generate these values itself.
* @since 5.6.0 The returned array of comment data is assigned to the `comments` property
* of the current WP_Comment_Query instance.
* @param array|int|null $comment_data Return an array of comment data to short-circuit WP's comment query,
* the comment count as an integer if `$this->query_vars['count']` is set,
* or null to allow WP to run its normal queries.
* @param WP_Comment_Query $query The WP_Comment_Query instance, passed by reference.
$comment_data = apply_filters_ref_array( 'comments_pre_query', array( $comment_data, &$this ) );
if ( null !== $comment_data ) {
if ( is_array( $comment_data ) && ! $this->query_vars['count'] ) {
$this->comments = $comment_data;
* Only use the args defined in the query_var_defaults to compute the key,
* but ignore 'fields', 'update_comment_meta_cache', 'update_comment_post_cache' which does not affect query results.
$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
unset( $_args['fields'], $_args['update_comment_meta_cache'], $_args['update_comment_post_cache'] );
$key = md5( serialize( $_args ) );
$last_changed = wp_cache_get_last_changed( 'comment' );
$cache_key = "get_comments:$key";
$cache_value = wp_cache_get_salted( $cache_key, 'comment-queries', $last_changed );
if ( false === $cache_value ) {
$comment_ids = $this->get_comment_ids();
$this->set_found_comments();
'comment_ids' => $comment_ids,
'found_comments' => $this->found_comments,
wp_cache_set_salted( $cache_key, $cache_value, 'comment-queries', $last_changed );
$comment_ids = $cache_value['comment_ids'];
$this->found_comments = $cache_value['found_comments'];
if ( $this->found_comments && $this->query_vars['number'] ) {
$this->max_num_pages = (int) ceil( $this->found_comments / $this->query_vars['number'] );
// If querying for a count only, there's nothing more to do.
if ( $this->query_vars['count'] ) {
// $comment_ids is actually a count in this case.
return (int) $comment_ids;
$comment_ids = array_map( 'intval', $comment_ids );
if ( $this->query_vars['update_comment_meta_cache'] ) {
wp_lazyload_comment_meta( $comment_ids );
if ( 'ids' === $this->query_vars['fields'] ) {
$this->comments = $comment_ids;
_prime_comment_caches( $comment_ids, false );
// Fetch full comment objects from the primed cache.
foreach ( $comment_ids as $comment_id ) {
$_comment = get_comment( $comment_id );
$_comments[] = $_comment;