From 1b016b6dc6c21cca1dad10a99284c81c59218f19 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 16 Sep 2015 18:49:28 +0000 Subject: [PATCH] Failed `get_term_by()` lookups should always return `false`. Previously, we sometimes returned `null`. Props charlestonsw, tyxla. Fixes #33281. git-svn-id: https://develop.svn.wordpress.org/trunk@34246 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy-functions.php | 3 ++- tests/phpunit/tests/term.php | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/taxonomy-functions.php b/src/wp-includes/taxonomy-functions.php index 8a80184197..eee37bc8a3 100644 --- a/src/wp-includes/taxonomy-functions.php +++ b/src/wp-includes/taxonomy-functions.php @@ -803,8 +803,9 @@ function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw $field = 'tt.term_taxonomy_id'; } else { $term = get_term( (int) $value, $taxonomy, $output, $filter ); - if ( is_wp_error( $term ) ) + if ( is_wp_error( $term ) || is_null( $term ) ) { $term = false; + } return $term; } diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 60409dda15..bd050c55b0 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -57,6 +57,14 @@ class Tests_Term extends WP_UnitTestCase { $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 ); } + /** + * @ticket 33281 + */ + function test_get_term_by_with_nonexistent_id_should_return_false() { + $term = get_term_by( 'id', 123456, 'category' ); + $this->assertFalse( $term ); + } + /** * @ticket 15919 */