Title: comments_pre_query
Published: November 12, 2019
Last modified: April 28, 2025

---

# apply_filters_ref_array( ‘comments_pre_query’, array|int|null $comment_data, WP_Comment_Query $query )

## In this article

 * [Description](https://developer.wordpress.org/reference/hooks/comments_pre_query/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/hooks/comments_pre_query/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/hooks/comments_pre_query/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/comments_pre_query/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/comments_pre_query/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/hooks/comments_pre_query/?output_format=md#wp--skip-link--target)

Filters the comments data before the query takes place.

## 󠀁[Description](https://developer.wordpress.org/reference/hooks/comments_pre_query/?output_format=md#description)󠁿

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](https://developer.wordpress.org/reference/classes/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](https://developer.wordpress.org/reference/classes/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](https://developer.wordpress.org/reference/classes/wp_comment_query/)
object, passed to the filter by reference. If [WP_Comment_Query](https://developer.wordpress.org/reference/classes/wp_comment_query/)
does not perform a database query, it will not have enough information to generate
these values itself.

## 󠀁[Parameters](https://developer.wordpress.org/reference/hooks/comments_pre_query/?output_format=md#parameters)󠁿

 `$comment_data`array|int|null

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.

`$query`[WP_Comment_Query](https://developer.wordpress.org/reference/classes/wp_comment_query/)

The [WP_Comment_Query](https://developer.wordpress.org/reference/classes/wp_comment_query/)
instance, passed by reference.

## 󠀁[Source](https://developer.wordpress.org/reference/hooks/comments_pre_query/?output_format=md#source)󠁿

    ```php
    $comment_data = apply_filters_ref_array( 'comments_pre_query', array( $comment_data, &$this ) );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-comment-query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-comment-query.php#L434)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-comment-query.php#L434-L434)

## 󠀁[Related](https://developer.wordpress.org/reference/hooks/comments_pre_query/?output_format=md#related)󠁿

| Used by | Description | 
| [WP_Comment_Query::get_comments()](https://developer.wordpress.org/reference/classes/wp_comment_query/get_comments/)`wp-includes/class-wp-comment-query.php` |

Get a list of comments matching the query vars.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/hooks/comments_pre_query/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.6.0](https://developer.wordpress.org/reference/since/5.6.0/) | The returned array of comment data is assigned to the `comments` property of the current [WP_Comment_Query](https://developer.wordpress.org/reference/classes/wp_comment_query/) instance. | 
| [5.3.0](https://developer.wordpress.org/reference/since/5.3.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomments_pre_query%2F)
before being able to contribute a note or feedback.