diff --git a/src/wp-includes/taxonomy-functions.php b/src/wp-includes/taxonomy-functions.php index 0c89ac1e51..88f944edc2 100644 --- a/src/wp-includes/taxonomy-functions.php +++ b/src/wp-includes/taxonomy-functions.php @@ -1155,6 +1155,9 @@ function get_terms( $taxonomies, $args = '' ) { $cache_key = "get_terms:$key:$last_changed"; $cache = wp_cache_get( $cache_key, 'terms' ); if ( false !== $cache ) { + if ( 'all' === $args['fields'] ) { + $cache = array_map( 'get_term', $cache ); + } /** * Filter the given taxonomy's terms cache. @@ -1494,8 +1497,6 @@ function get_terms( $taxonomies, $args = '' ) { foreach ( $terms as $term ) { $_terms[ $term->term_id ] = $term->slug; } - } else { - $_terms = array_map( 'get_term', $terms ); } if ( ! empty( $_terms ) ) { @@ -1508,6 +1509,10 @@ function get_terms( $taxonomies, $args = '' ) { wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS ); + if ( 'all' === $_fields ) { + $terms = array_map( 'get_term', $terms ); + } + /** This filter is documented in wp-includes/taxonomy */ return apply_filters( 'get_terms', $terms, $taxonomies, $args ); } diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 1c42e49ed7..0b7ae77e9b 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -1591,6 +1591,38 @@ class Tests_Term_getTerms extends WP_UnitTestCase { } } + /** + * @ticket 14162 + * @ticket 34282 + */ + public function test_should_return_wp_term_objects_when_pulling_from_the_cache() { + global $wpdb; + + register_taxonomy( 'wptests_tax', 'post' ); + $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) ); + + // Prime the cache. + get_terms( 'wptests_tax', array( + 'hide_empty' => false, + 'fields' => 'all', + ) ); + + $num_queries = $wpdb->num_queries; + + $found = get_terms( 'wptests_tax', array( + 'hide_empty' => false, + 'fields' => 'all', + ) ); + + $this->assertSame( $num_queries, $wpdb->num_queries ); + + $this->assertNotEmpty( $found ); + + foreach ( $found as $term ) { + $this->assertInstanceOf( 'WP_Term', $term ); + } + } + /** * @ticket 14162 */