Make WordPress Core


Ignore:
Timestamp:
09/29/2015 03:51:11 AM (10 years ago)
Author:
boonebgorges
Message:

Don't require explicit taxonomy when getting terms by term_taxonomy_id.

Props wonderboymusic.
Fixes #30620.

File:
1 edited

Legend:

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

    r34538 r34679  
    782782 *
    783783 * @since 2.3.0
     784 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'.
    784785 *
    785786 * @global wpdb $wpdb WordPress database abstraction object.
     
    788789 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
    789790 * @param string|int $value    Search for this term value
    790  * @param string     $taxonomy Taxonomy Name
     791 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
    791792 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
    792793 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
     
    794795 *                                          Will return false if $taxonomy does not exist or $term was not found.
    795796 */
    796 function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
     797function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
    797798    global $wpdb;
    798799
    799     if ( ! taxonomy_exists($taxonomy) )
     800    // 'term_taxonomy_id' lookups don't require taxonomy checks.
     801    if ( 'term_taxonomy_id' !== $field && ! taxonomy_exists( $taxonomy ) ) {
    800802        return false;
     803    }
     804
     805    $tax_clause = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
    801806
    802807    if ( 'slug' == $field ) {
     
    812817        $value = (int) $value;
    813818        $field = 'tt.term_taxonomy_id';
     819
     820        // No `taxonomy` clause when searching by 'term_taxonomy_id'.
     821        $tax_clause = '';
    814822    } else {
    815823        $term = get_term( (int) $value, $taxonomy, $output, $filter );
     
    820828    }
    821829
    822     $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value ) );
     830    $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE $field = %s $tax_clause LIMIT 1", $value ) );
    823831    if ( ! $term )
    824832        return false;
     833
     834    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
     835    if ( 'term_taxonomy_id' === $field ) {
     836        $taxonomy = $term->taxonomy;
     837    }
    825838
    826839    wp_cache_add( $term->term_id, $term, $taxonomy );
Note: See TracChangeset for help on using the changeset viewer.