WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/29/2016 10:35:32 PM (4 years ago)
Author:
ocean90
Message:

Taxonomy: Use WP_Term_Query in get_term_by().

WP_Term_Query already supports querying terms by 'slug', 'name', and 'term_taxonomy_id'. Its additional arguments allow us to generate nearly the same SQL queries as before.
This change has one yuge benefit: the term queries are now cached.

Add tests to increase coverage of get_term_by().

Props spacedmonkey, boonebgorges, johnjamesjacoby, pento, ocean90.
Fixes #21760.

File:
1 edited

Legend:

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

    r38667 r38677  
    928928 */
    929929function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
    930     global $wpdb;
    931930
    932931    // 'term_taxonomy_id' lookups don't require taxonomy checks.
     
    935934    }
    936935
    937     $tax_clause = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
    938 
    939     if ( 'slug' == $field ) {
    940         $_field = 't.slug';
    941         $value = sanitize_title($value);
    942         if ( empty($value) )
     936    if ( 'id' === $field || 'term_id' === $field ) {
     937        $term = get_term( (int) $value, $taxonomy, $output, $filter );
     938        if ( is_wp_error( $term ) || null === $term ) {
     939            $term = false;
     940        }
     941        return $term;
     942    }
     943
     944    $args = array(
     945        'get'                    => 'all',
     946        'number'                 => 1,
     947        'taxonomy'               => $taxonomy,
     948        'update_term_meta_cache' => false,
     949        'orderby'                => 'none',
     950    );
     951
     952    switch ( $field ) {
     953        case 'slug' :
     954            $args['slug'] = $value;
     955            break;
     956        case 'name' :
     957            $args['name'] = $value;
     958            break;
     959        case 'term_taxonomy_id' :
     960            $args['term_taxonomy_id'] = $value;
     961            unset( $args[ 'taxonomy' ] );
     962            break;
     963        default :
    943964            return false;
    944     } elseif ( 'name' == $field ) {
    945         // Assume already escaped
    946         $value = wp_unslash($value);
    947         $_field = 't.name';
    948     } elseif ( 'term_taxonomy_id' == $field ) {
    949         $value = (int) $value;
    950         $_field = 'tt.term_taxonomy_id';
    951 
    952         // No `taxonomy` clause when searching by 'term_taxonomy_id'.
    953         $tax_clause = '';
    954     } else {
    955         $term = get_term( (int) $value, $taxonomy, $output, $filter );
    956         if ( is_wp_error( $term ) || is_null( $term ) ) {
    957             $term = false;
    958         }
    959         return $term;
    960     }
    961 
    962     $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", $value ) . " $tax_clause LIMIT 1" );
    963     if ( ! $term )
     965    }
     966
     967    $terms = get_terms( $args );
     968    if ( is_wp_error( $terms ) || empty( $terms ) ) {
    964969        return false;
     970    }
     971
     972    $term = array_shift( $terms );
    965973
    966974    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
     
    968976        $taxonomy = $term->taxonomy;
    969977    }
    970 
    971     wp_cache_add( $term->term_id, $term, 'terms' );
    972978
    973979    return get_term( $term, $taxonomy, $output, $filter );
Note: See TracChangeset for help on using the changeset viewer.