diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 16bdf26ae8..ccaae9041b 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -3783,7 +3783,7 @@ function update_object_term_cache($object_ids, $object_type) { $object_terms = array(); foreach ( (array) $terms as $term ) - $object_terms[$term->object_id][$term->taxonomy][$term->term_id] = $term; + $object_terms[$term->object_id][$term->taxonomy][] = $term; foreach ( $ids as $id ) { foreach ( $taxonomies as $taxonomy ) { diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 78f4d29dc0..588b1dc9c8 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -1584,6 +1584,35 @@ class Tests_Term extends WP_UnitTestCase { $this->assertEquals( 'This description is even more amazing!', $terms[0]->description ); } + /** + * @ticket 31086 + */ + public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_empty() { + register_taxonomy( 'wptests_tax', 'post' ); + $p = $this->factory->post->create(); + wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' ); + + $found = get_the_terms( $p, 'wptests_tax' ); + + $this->assertEqualSets( array( 0, 1 ), array_keys( $found ) ); + } + + /** + * @ticket 31086 + */ + public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_primed() { + register_taxonomy( 'wptests_tax', 'post' ); + $p = $this->factory->post->create(); + wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' ); + + // Prime cache. + update_object_term_cache( array( $p ), array( 'post' ) ); + + $found = get_the_terms( $p, 'wptests_tax' ); + + $this->assertEqualSets( array( 0, 1 ), array_keys( $found ) ); + } + /** * @ticket 19205 */