Make WordPress Core

Changeset 47865


Ignore:
Timestamp:
05/29/2020 10:41:25 PM (4 years ago)
Author:
whyisjake
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r47611 r47865  
    914914 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
    915915 *              a WP_Term object if `$output` is `OBJECT`.
     916 * @since 5.5.0 ID is an alias of id.
    916917 *
    917918 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
    918919 *
    919  * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
     920 * @param string     $field    Either 'slug', 'name', 'id' (term_id or ID), or 'term_taxonomy_id'
    920921 * @param string|int $value    Search for this term value
    921922 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
     
    942943    }
    943944
    944     if ( 'id' === $field || 'term_id' === $field ) {
     945    if ( 'id' === $field || 'term_id' === $field || 'ID' === $field ) {
    945946        $term = get_term( (int) $value, $taxonomy, $output, $filter );
    946947        if ( is_wp_error( $term ) || null === $term ) {
  • trunk/tests/phpunit/tests/term/getTermBy.php

    r46586 r47865  
    3232        $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
    3333    }
     34
     35    /**
     36     * @ticket 45163
     37     */
     38    function test_get_term_by_uppercase_id() {
     39        $term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
     40        $term2 = get_term_by( 'ID', $term1['term_id'], 'category' );
     41        $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
     42    }   
    3443
    3544    /**
Note: See TracChangeset for help on using the changeset viewer.