comments_pre_query
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.
Usage
add_filter( 'comments_pre_query', 'wp_kama_comments_pre_query_filter' ); /** * Function for `comments_pre_query` filter-hook. * * @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. * * @return array|int|null */ function wp_kama_comments_pre_query_filter( $comment_data ){ // filter... return $comment_data; }
- $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.
Changelog
Since 5.3.0 | Introduced. |
Since 5.6.0 | The returned array of comment data is assigned to the comments property of the current WP_Comment_Query instance. |
Where the hook is called
wp-includes/class-wp-comment-query.php 434
$comment_data = apply_filters_ref_array( 'comments_pre_query', array( $comment_data, &$this ) );