diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index fa32393b45..6ddfeb00a2 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -954,7 +954,7 @@ function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') { * @uses sanitize_term() Cleanses the term based on $filter context before returning. * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. * - * @param string $field Either 'slug', 'name', or 'id' + * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id' * @param string|int $value Search for this term value * @param string $taxonomy Taxonomy Name * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N @@ -976,6 +976,9 @@ function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw // Assume already escaped $value = wp_unslash($value); $field = 't.name'; + } else if ( 'term_taxonomy_id' == $field ) { + $value = (int) $value; + $field = 'tt.term_taxonomy_id'; } else { $term = get_term( (int) $value, $taxonomy, $output, $filter); if ( is_wp_error( $term ) ) diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index ff3fd8a541..6b35f4e2f8 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -489,7 +489,7 @@ class Tests_Term extends WP_UnitTestCase { $this->assertEquals( array( $term4['term_id'] ), $post->post_category ); wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ), true ); - $this->assertEquals( array( $term2['term_id'], $term4['term_id'],$term1['term_id'] ), $post->post_category ); + $this->assertEquals( array( $term2['term_id'], $term4['term_id'], $term1['term_id'] ), $post->post_category ); wp_set_post_categories( $post_id, array(), true ); $this->assertEquals( 1, count( $post->post_category ) ); @@ -499,4 +499,10 @@ class Tests_Term extends WP_UnitTestCase { $this->assertEquals( 1, count( $post->post_category ) ); $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); } + + function test_get_term_by_tt_id() { + $term1 = wp_insert_term( 'Foo', 'category' ); + $term2 = get_term_by( 'term_taxonomy_id', $term1['term_taxonomy_id'], 'category' ); + $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 ); + } }