diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index bf3db0ecf3..0531c00b7b 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/wp-includes/class-wp-comment-query.php @@ -444,10 +444,10 @@ class WP_Comment_Query { /* * Only use the args defined in the query_var_defaults to compute the key, - * but ignore 'fields', which does not affect query results. + * but ignore 'fields', 'update_comment_meta_cache', 'update_comment_post_cache' which does not affect query results. */ $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); - unset( $_args['fields'] ); + unset( $_args['fields'], $_args['update_comment_meta_cache'], $_args['update_comment_post_cache'] ); $key = md5( serialize( $_args ) ); $last_changed = wp_cache_get_last_changed( 'comment' ); diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 68265dfdac..9b993d2c25 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -4941,4 +4941,31 @@ class Tests_Comment_Query extends WP_UnitTestCase { return array( get_comment( $c ) ); } + + /** + * @ticket 55460 + */ + public function test_comment_cache_key_should_ignore_unset_params() { + $p = self::factory()->post->create(); + $c = self::factory()->comment->create( array( 'comment_post_ID' => $p ) ); + + $_args = array( + 'post_id' => $p, + 'fields' => 'ids', + 'update_comment_meta_cache' => true, + 'update_comment_post_cache' => false, + ); + + $q1 = new WP_Comment_Query(); + $q1->query( $_args ); + + $num_queries_all_args = get_num_queries(); + + // Ignore 'fields', 'update_comment_meta_cache', 'update_comment_post_cache'. + unset( $_args['fields'], $_args['update_comment_meta_cache'], $_args['update_comment_post_cache'] ); + $q2 = new WP_Comment_Query(); + $q2->query( $_args ); + + $this->assertSame( $num_queries_all_args, get_num_queries() ); + } }