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. Built from https://develop.svn.wordpress.org/trunk@38677 git-svn-id: http://core.svn.wordpress.org/trunk@38620 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
e53b79b726
commit
324748a854
|
@ -927,49 +927,55 @@ function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
|
|||
* or `$term` was not found.
|
||||
*/
|
||||
function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
|
||||
global $wpdb;
|
||||
|
||||
// 'term_taxonomy_id' lookups don't require taxonomy checks.
|
||||
if ( 'term_taxonomy_id' !== $field && ! taxonomy_exists( $taxonomy ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$tax_clause = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
|
||||
|
||||
if ( 'slug' == $field ) {
|
||||
$_field = 't.slug';
|
||||
$value = sanitize_title($value);
|
||||
if ( empty($value) )
|
||||
return false;
|
||||
} elseif ( 'name' == $field ) {
|
||||
// Assume already escaped
|
||||
$value = wp_unslash($value);
|
||||
$_field = 't.name';
|
||||
} elseif ( 'term_taxonomy_id' == $field ) {
|
||||
$value = (int) $value;
|
||||
$_field = 'tt.term_taxonomy_id';
|
||||
|
||||
// No `taxonomy` clause when searching by 'term_taxonomy_id'.
|
||||
$tax_clause = '';
|
||||
} else {
|
||||
if ( 'id' === $field || 'term_id' === $field ) {
|
||||
$term = get_term( (int) $value, $taxonomy, $output, $filter );
|
||||
if ( is_wp_error( $term ) || is_null( $term ) ) {
|
||||
if ( is_wp_error( $term ) || null === $term ) {
|
||||
$term = false;
|
||||
}
|
||||
return $term;
|
||||
}
|
||||
|
||||
$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" );
|
||||
if ( ! $term )
|
||||
$args = array(
|
||||
'get' => 'all',
|
||||
'number' => 1,
|
||||
'taxonomy' => $taxonomy,
|
||||
'update_term_meta_cache' => false,
|
||||
'orderby' => 'none',
|
||||
);
|
||||
|
||||
switch ( $field ) {
|
||||
case 'slug' :
|
||||
$args['slug'] = $value;
|
||||
break;
|
||||
case 'name' :
|
||||
$args['name'] = $value;
|
||||
break;
|
||||
case 'term_taxonomy_id' :
|
||||
$args['term_taxonomy_id'] = $value;
|
||||
unset( $args[ 'taxonomy' ] );
|
||||
break;
|
||||
default :
|
||||
return false;
|
||||
}
|
||||
|
||||
$terms = get_terms( $args );
|
||||
if ( is_wp_error( $terms ) || empty( $terms ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$term = array_shift( $terms );
|
||||
|
||||
// In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
|
||||
if ( 'term_taxonomy_id' === $field ) {
|
||||
$taxonomy = $term->taxonomy;
|
||||
}
|
||||
|
||||
wp_cache_add( $term->term_id, $term, 'terms' );
|
||||
|
||||
return get_term( $term, $taxonomy, $output, $filter );
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '4.7-alpha-38676';
|
||||
$wp_version = '4.7-alpha-38677';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue