diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 93df284fc6..107f694ba1 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1383,19 +1383,26 @@ function get_terms($taxonomies, $args = '') { $selects = array(); switch ( $fields ) { case 'all': - $selects = array('t.*', 'tt.*'); + $selects = array( 't.*', 'tt.*' ); break; case 'ids': case 'id=>parent': - $selects = array('t.term_id', 'tt.parent', 'tt.count'); + $selects = array( 't.term_id', 'tt.parent', 'tt.count' ); break; case 'names': - $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name'); + $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name' ); break; case 'count': $orderby = ''; $order = ''; - $selects = array('COUNT(*)'); + $selects = array( 'COUNT(*)' ); + break; + case 'id=>name': + $selects = array( 't.term_id', 't.name' ); + break; + case 'id=>slug': + $selects = array( 't.term_id', 't.slug' ); + break; } $_fields = $fields; @@ -1454,29 +1461,35 @@ function get_terms($taxonomies, $args = '') { } } } - reset ( $terms ); + reset( $terms ); $_terms = array(); if ( 'id=>parent' == $fields ) { - while ( $term = array_shift($terms) ) + while ( $term = array_shift( $terms ) ) $_terms[$term->term_id] = $term->parent; - $terms = $_terms; } elseif ( 'ids' == $fields ) { - while ( $term = array_shift($terms) ) + while ( $term = array_shift( $terms ) ) $_terms[] = $term->term_id; - $terms = $_terms; } elseif ( 'names' == $fields ) { - while ( $term = array_shift($terms) ) + while ( $term = array_shift( $terms ) ) $_terms[] = $term->name; - $terms = $_terms; + } elseif ( 'id=>name' == $fields ) { + while ( $term = array_shift( $terms ) ) + $_terms[$term->term_id] = $term->name; + } elseif ( 'id=>slug' == $fields ) { + while ( $term = array_shift( $terms ) ) + $_terms[$term->term_id] = $term->slug; } + if ( ! empty( $_terms ) ) + $terms = $_terms; + if ( $number && is_array( $terms ) && count( $terms ) > $number ) $terms = array_slice( $terms, $offset, $number ); wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS ); - $terms = apply_filters('get_terms', $terms, $taxonomies, $args); + $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args ); return $terms; } diff --git a/tests/tests/term/getTerms.php b/tests/tests/term/getTerms.php index 6220e9d8aa..75523b0d2c 100644 --- a/tests/tests/term/getTerms.php +++ b/tests/tests/term/getTerms.php @@ -87,4 +87,36 @@ class Tests_Term_getTerms extends WP_UnitTestCase { $terms = get_terms( array( '111' => 'post_tag' ), array( 'hide_empty' => false ) ); $this->assertEquals( $term_id, $terms[0]->term_id ); } + + /** + * @ticket 13661 + */ + function test_get_terms_fields() { + $term_id1 = $this->factory->tag->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) ); + $term_id2 = $this->factory->tag->create( array( 'slug' => 'hoo', 'name' => 'HOO!', 'parent' => $term_id1 ) ); + + $terms_id_parent = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>parent' ) ); + $this->assertEquals( array( + $term_id1 => 0, + $term_id2 => $term_id1 + ), $terms_id_parent ); + + $terms_ids = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'ids' ) ); + $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms_ids ); + + $terms_name = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'names' ) ); + $this->assertEqualSets( array( 'WOO!', 'HOO!' ), $terms_name ); + + $terms_id_name = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>name' ) ); + $this->assertEquals( array( + $term_id1 => 'WOO!', + $term_id2 => 'HOO!', + ), $terms_id_name ); + + $terms_id_slug = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>slug' ) ); + $this->assertEquals( array( + $term_id1 => 'woo', + $term_id2 => 'hoo' + ), $terms_id_slug ); + } }