From 6e66f08ec9fc97b8342871f93d0a3fb2ea1d4048 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 28 Jul 2020 14:21:54 +0000 Subject: [PATCH] Taxonomy: Ensure the `child_of` argument of `get_terms()` works as expected with `'fields' => 'id=>name'` or `'id=>slug'`. Props Howdy_McGee, deepaklalwani, planvova. Fixes #46768. git-svn-id: https://develop.svn.wordpress.org/trunk@48663 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-term-query.php | 4 +- tests/phpunit/tests/term/getTerms.php | 77 +++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index 8b8fea4c9b..15198a3356 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/wp-includes/class-wp-term-query.php @@ -620,10 +620,10 @@ class WP_Term_Query { $selects = array( 'COUNT(*)' ); break; case 'id=>name': - $selects = array( 't.term_id', 't.name', 'tt.count', 'tt.taxonomy' ); + $selects = array( 't.term_id', 't.name', 'tt.parent', 'tt.count', 'tt.taxonomy' ); break; case 'id=>slug': - $selects = array( 't.term_id', 't.slug', 'tt.count', 'tt.taxonomy' ); + $selects = array( 't.term_id', 't.slug', 'tt.parent', 'tt.count', 'tt.taxonomy' ); break; } diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 2c20a191eb..a22f90a7a9 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -719,6 +719,83 @@ class Tests_Term_getTerms extends WP_UnitTestCase { $this->assertEquals( 1, count( $terms ) ); } + /** + * @ticket 46768 + */ + function test_get_terms_child_of_fields_id_name() { + $parent = self::factory()->category->create(); + $child = self::factory()->category->create( + array( + 'parent' => $parent, + 'slug' => 'test-1', + 'name' => 'Test 1', + ) + ); + $child2 = self::factory()->category->create( + array( + 'parent' => $parent, + 'slug' => 'test-2', + 'name' => 'Test 2', + ) + ); + + $terms = get_terms( + 'category', + array( + 'child_of' => $parent, + 'hide_empty' => false, + 'fields' => 'id=>name', + ) + ); + + $this->assertEquals( + array( + $child => 'Test 1', + $child2 => 'Test 2', + ), + $terms + ); + + } + + /** + * @ticket 46768 + */ + function test_get_terms_child_of_fields_id_slug() { + $parent = self::factory()->category->create(); + $child = self::factory()->category->create( + array( + 'parent' => $parent, + 'slug' => 'test-1', + 'name' => 'Test 1', + ) + ); + $child2 = self::factory()->category->create( + array( + 'parent' => $parent, + 'slug' => 'test-2', + 'name' => 'Test 2', + ) + ); + + $terms = get_terms( + 'category', + array( + 'child_of' => $parent, + 'hide_empty' => false, + 'fields' => 'id=>slug', + ) + ); + + $this->assertEquals( + array( + $child => 'test-1', + $child2 => 'test-2', + ), + $terms + ); + } + /** * @ticket 31118 */