- $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $parent_ids ) ) . ')';
- $comment_ids = $wpdb->get_col( "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );
+ if ( $uncached_parent_ids ) {
+ $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')';
+ $level_comments = $wpdb->get_results( "SELECT $wpdb->comments.comment_ID, $wpdb->comments.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );
+
+ // Cache parent-child relationships.
+ $parent_map = array_fill_keys( $uncached_parent_ids, array() );
+ foreach ( $level_comments as $level_comment ) {
+ $parent_map[ $level_comment->comment_parent ][] = $level_comment->comment_ID;
+ $child_ids[] = $level_comment->comment_ID;
+ }
+
+ foreach ( $parent_map as $parent_id => $children ) {
+ $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed";
+ wp_cache_set( $cache_key, $children, 'comment' );
+ }
+ }