From fa21c5e6755b1af92dd982df800db51b5d762ce0 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 3 Aug 2016 13:50:14 +0000 Subject: [PATCH] In `WP_Term_Query`, accept a string value for `taxonomy`. Props endocreative. Props ocean90 for review. Fixes #37545. git-svn-id: https://develop.svn.wordpress.org/trunk@38181 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-term-query.php | 2 +- tests/phpunit/tests/term/query.php | 35 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index 2d3d085af3..565c1e6268 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/wp-includes/class-wp-term-query.php @@ -219,7 +219,7 @@ class WP_Term_Query { $query = $this->query_vars; } - $taxonomies = isset( $query['taxonomy'] ) ? $query['taxonomy'] : null; + $taxonomies = isset( $query['taxonomy'] ) ? (array) $query['taxonomy'] : null; /** * Filters the terms query default arguments. diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php index 9a5a29583f..8e80bb4fb1 100644 --- a/tests/phpunit/tests/term/query.php +++ b/tests/phpunit/tests/term/query.php @@ -4,6 +4,41 @@ * @group taxonomy */ class Tests_Term_Query extends WP_UnitTestCase { + /** + * @ticket 37545 + */ + public function test_taxonomy_should_accept_single_taxonomy_as_string() { + register_taxonomy( 'wptests_tax_1', 'post' ); + register_taxonomy( 'wptests_tax_2', 'post' ); + + $term_1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) ); + $term_2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_2' ) ); + + $q = new WP_Term_Query( array( + 'taxonomy' => 'wptests_tax_2', + 'fields' => 'ids', + 'hide_empty' => false, + ) ); + + $this->assertEqualSets( array( $term_2 ), $q->terms ); + } + + public function test_taxonomy_should_accept_taxonomy_array() { + register_taxonomy( 'wptests_tax_1', 'post' ); + register_taxonomy( 'wptests_tax_2', 'post' ); + + $term_1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) ); + $term_2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_2' ) ); + + $q = new WP_Term_Query( array( + 'taxonomy' => array( 'wptests_tax_2' ), + 'fields' => 'ids', + 'hide_empty' => false, + ) ); + + $this->assertEqualSets( array( $term_2 ), $q->terms ); + } + /** * @ticket 37074 */