diff --git a/wp-includes/class-wp-comment-query.php b/wp-includes/class-wp-comment-query.php index ab49452ed8..6d215615d8 100644 --- a/wp-includes/class-wp-comment-query.php +++ b/wp-includes/class-wp-comment-query.php @@ -922,6 +922,11 @@ class WP_Comment_Query { } } + // Set the 'populated_children' flag, to ensure additional database queries aren't run. + foreach ( $ref as $_ref ) { + $_ref->populated_children( true ); + } + $comments = $threaded_comments; } else { $comments = $all_comments; diff --git a/wp-includes/class-wp-comment.php b/wp-includes/class-wp-comment.php index 8dc9e9413b..6319ad4764 100644 --- a/wp-includes/class-wp-comment.php +++ b/wp-includes/class-wp-comment.php @@ -158,6 +158,15 @@ final class WP_Comment { */ protected $children; + /** + * Whether children have been populated for this comment object. + * + * @since 4.4.0 + * @access protected + * @var bool + */ + protected $populated_children = false; + /** * Post fields. * @@ -279,7 +288,11 @@ final class WP_Comment { $_args['parent'] = $this->comment_ID; if ( is_null( $this->children ) ) { - $this->children = get_comments( $_args ); + if ( $this->populated_children ) { + $this->children = array(); + } else { + $this->children = get_comments( $_args ); + } } if ( 'flat' === $_args['format'] ) { @@ -330,6 +343,18 @@ final class WP_Comment { return false; } + /** + * Set the 'populated_children' flag. + * + * This flag is important for ensuring that calling `get_children()` on a childless comment will not trigger + * unneeded database queries. + * + * @since 4.4.0 + */ + public function populated_children( $set ) { + $this->populated_children = (bool) $set; + } + /** * Check whether a non-public property is set. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 6676fe9e40..6ba61c2dad 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.4-alpha-34729'; +$wp_version = '4.4-alpha-34730'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.