Add `term_taxonomy_id` to available fields in `get_term_by()`. Adds unit test.

Props jchristopher.
Fixes #21651.



git-svn-id: https://develop.svn.wordpress.org/trunk@25334 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2013-09-11 00:01:19 +00:00
parent a963ab0852
commit 0c0c81d81c
2 changed files with 11 additions and 2 deletions

View File

@ -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 ) )

View File

@ -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 );
}
}