Changeset 38677 for trunk/src/wp-includes/taxonomy.php
- Timestamp:
- 09/29/2016 10:35:32 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/taxonomy.php
r38667 r38677 928 928 */ 929 929 function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { 930 global $wpdb;931 930 932 931 // 'term_taxonomy_id' lookups don't require taxonomy checks. … … 935 934 } 936 935 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 : 943 964 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 ) ) { 964 969 return false; 970 } 971 972 $term = array_shift( $terms ); 965 973 966 974 // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db. … … 968 976 $taxonomy = $term->taxonomy; 969 977 } 970 971 wp_cache_add( $term->term_id, $term, 'terms' );972 978 973 979 return get_term( $term, $taxonomy, $output, $filter );
Note: See TracChangeset
for help on using the changeset viewer.