Don't use term IDs for array indexes when caching object terms.
Uncached results pulled from `wp_get_object_terms()` are zero-indexed (ie 0, 1, 2...). As a result, `get_the_terms()` was returning a strictly different array when pulling from the cache and when the cache was empty. Props joshlevinson. Fixes #31086. git-svn-id: https://develop.svn.wordpress.org/trunk@31287 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
a57a612c95
commit
2e848072eb
|
@ -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 ) {
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue