[wp-trac] [WordPress Trac] #33281: get_term_by() not returning false if ID does not exist
WordPress Trac
noreply at wordpress.org
Wed Aug 5 17:43:36 UTC 2015
#33281: get_term_by() not returning false if ID does not exist
--------------------------+-----------------------------
Reporter: charlestonsw | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Taxonomy | Version: trunk
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
get_term_by() should return false if a specified ID does not exist within
a custom taxonomy. Instead an empty array (null) is returned.
Example:
{{{
...
$category_array = get_term_by( 'id' , $term_id , SLPlus::locationTaxonomy,
ARRAY_A );
if ( $category_array === false ) {
$this->debugMP('msg', "Tagalong term ID {$term_id} does not exist." );
}
}}}
When passing term_id value of 2, which does not exist in the terms tables,
this should return false. Instead null is returned.
The issue originates in get_term() which returns null in two cases versus
throwing a WP_Error.
From get_term()
{{{
...
if ( is_object($term) )
$term = $term->term_id;
if ( !$term = (int) $term )
return null;
if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) {
$_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 t.term_id = %d LIMIT 1",
$taxonomy, $term) );
if ( ! $_term )
return null;
...
}}}
In those two cases, one of which is exactly the "term ID does not exist in
the specified taxonomy" get_term_by should return false. However the
return logic does not catch the null value:
From get_term_by():
{{{
...
$term = get_term( (int) $value, $taxonomy, $output,
$filter );
if ( is_wp_error( $term ) )
$term = false;
return $term;
...
}}}
This should be:
{{{
...
$term = get_term( (int) $value, $taxonomy, $output,
$filter );
if ( is_wp_error( $term ) || is_null( $term ) )
$term = false;
return $term;
...
}}}
WP Version: You are using a development version (4.3-RC2-33572-src).
--
Ticket URL: <https://core.trac.wordpress.org/ticket/33281>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list