diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 5f5127ac10..1f51ff9cd2 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -3721,7 +3721,7 @@ function update_object_term_cache($object_ids, $object_type) { function update_term_cache( $terms, $taxonomy = '' ) { foreach ( (array) $terms as $term ) { // Create a copy in case the array was passed by reference. - $_term = $term; + $_term = clone $term; // Object ID should not be cached. unset( $_term->object_id ); diff --git a/tests/phpunit/tests/term/cache.php b/tests/phpunit/tests/term/cache.php index 828917aa99..8d9f51ada2 100644 --- a/tests/phpunit/tests/term/cache.php +++ b/tests/phpunit/tests/term/cache.php @@ -200,4 +200,23 @@ class Tests_Term_Cache extends WP_UnitTestCase { _unregister_taxonomy( 'wptests_tax' ); } + + /** + * @ticket 35462 + */ + public function test_term_objects_should_not_be_modified_by_update_term_cache() { + register_taxonomy( 'wptests_tax', 'post' ); + $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); + $p = self::factory()->post->create(); + + wp_set_object_terms( $p, $t, 'wptests_tax' ); + + $terms = wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'all_with_object_id' ) ); + + update_term_cache( $terms ); + + foreach ( $terms as $term ) { + $this->assertSame( $p, $term->object_id ); + } + } }