WP_Comment_Query::get_comments()
Get a list of comments matching the query vars.
Return
(int|int[]|WP_Comment[]) List of comments or number of found comments if $count
argument is true.
Source
File: wp-includes/class-wp-comment-query.php
public function get_comments() {
global $wpdb;
$this->parse_query();
// Parse meta query
$this->meta_query = new WP_Meta_Query();
$this->meta_query->parse_query_vars( $this->query_vars );
/**
* Fires before comments are retrieved.
*
* @since WP-3.1.0
*
* @param WP_Comment_Query $this 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 );
}
/*
* Only use the args defined in the query_var_defaults to compute the key,
* but ignore 'fields', which does not affect query results.
*/
$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
unset( $_args['fields'] );
$key = md5( serialize( $_args ) );
$last_changed = wp_cache_get_last_changed( 'comment' );
$cache_key = "get_comments:$key:$last_changed";
$cache_value = wp_cache_get( $cache_key, 'comment' );
if ( false === $cache_value ) {
$comment_ids = $this->get_comment_ids();
if ( $comment_ids ) {
$this->set_found_comments();
}
$cache_value = array(
'comment_ids' => $comment_ids,
'found_comments' => $this->found_comments,
);
wp_cache_add( $cache_key, $cache_value, 'comment' );
} else {
$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 = 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 intval( $comment_ids );
}
$comment_ids = array_map( 'intval', $comment_ids );
if ( 'ids' == $this->query_vars['fields'] ) {
$this->comments = $comment_ids;
return $this->comments;
}
_prime_comment_caches( $comment_ids, $this->query_vars['update_comment_meta_cache'] );
// Fetch full comment objects from the primed cache.
$_comments = array();
foreach ( $comment_ids as $comment_id ) {
if ( $_comment = get_comment( $comment_id ) ) {
$_comments[] = $_comment;
}
}
// Prime comment post caches.
if ( $this->query_vars['update_comment_post_cache'] ) {
$comment_post_ids = array();
foreach ( $_comments as $_comment ) {
$comment_post_ids[] = $_comment->comment_post_ID;
}
_prime_post_caches( $comment_post_ids, false, false );
}
/**
* Filters the comment query results.
*
* @since WP-3.1.0
*
* @param array $_comments An array of comments.
* @param WP_Comment_Query $this Current instance of WP_Comment_Query (passed by reference).
*/
$_comments = apply_filters_ref_array( 'the_comments', array( $_comments, &$this ) );
// Convert to WP_Comment instances
$comments = array_map( 'get_comment', $_comments );
if ( $this->query_vars['hierarchical'] ) {
$comments = $this->fill_descendants( $comments );
}
$this->comments = $comments;
return $this->comments;
}
Changelog
Version | Description |
---|---|
4.2.0 | Introduced. |