From 7b13f5abd807644a29f9765eb3e9c05b6c289dc5 Mon Sep 17 00:00:00 2001 From: Jake Spurlock Date: Fri, 29 May 2020 22:41:25 +0000 Subject: [PATCH] Taxonomy: Extend `get_term_by` to accept `ID` as a term parameter. Similar to `get_user_by`, both `ID` and `id` should be able to accepted. Fixes #45163. Props emrikol, esoj. git-svn-id: https://develop.svn.wordpress.org/trunk@47865 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 5 +++-- tests/phpunit/tests/term/getTermBy.php | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 3dc33122d8..a53377a415 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -913,10 +913,11 @@ function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { * @since 2.3.0 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return * a WP_Term object if `$output` is `OBJECT`. + * @since 5.5.0 ID is an alias of id. * * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. * - * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id' + * @param string $field Either 'slug', 'name', 'id' (term_id or ID), or 'term_taxonomy_id' * @param string|int $value Search for this term value * @param string $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to @@ -941,7 +942,7 @@ function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter } } - if ( 'id' === $field || 'term_id' === $field ) { + if ( 'id' === $field || 'term_id' === $field || 'ID' === $field ) { $term = get_term( (int) $value, $taxonomy, $output, $filter ); if ( is_wp_error( $term ) || null === $term ) { $term = false; diff --git a/tests/phpunit/tests/term/getTermBy.php b/tests/phpunit/tests/term/getTermBy.php index caf5a6ea98..d49f302877 100644 --- a/tests/phpunit/tests/term/getTermBy.php +++ b/tests/phpunit/tests/term/getTermBy.php @@ -32,6 +32,15 @@ class Tests_Term_GetTermBy extends WP_UnitTestCase { $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 ); } + /** + * @ticket 45163 + */ + function test_get_term_by_uppercase_id() { + $term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) ); + $term2 = get_term_by( 'ID', $term1['term_id'], 'category' ); + $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 ); + } + /** * @ticket 21651 */