From 325b1bf1751716c883ef5b814cf8e476f7a90c6e Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sat, 21 Sep 2013 16:47:43 +0000 Subject: [PATCH] Ensure that terms with a post count of `0` are not returned in `wp_count_terms()` when `hide_empty => true`. Adds unit test which ensures that `wp_count_terms()` returns `0`. Props markjaquith. Fixes #15919. git-svn-id: https://develop.svn.wordpress.org/trunk@25551 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 3 +++ tests/phpunit/tests/term.php | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 9ae219f688..8e1898e29f 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1369,6 +1369,9 @@ function get_terms($taxonomies, $args = '') { $where .= " AND tt.parent = '$parent'"; } + if ( 'count' == $fields ) + $hierarchical = false; + if ( $hide_empty && !$hierarchical ) $where .= ' AND tt.count > 0'; diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 6b35f4e2f8..40567b5cef 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -505,4 +505,11 @@ class Tests_Term extends WP_UnitTestCase { $term2 = get_term_by( 'term_taxonomy_id', $term1['term_taxonomy_id'], 'category' ); $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 ); } + + function test_wp_count_terms() { + $count = wp_count_terms( 'category', array( 'hide_empty' => true ) ); + // the terms inserted in setUp aren't attached to any posts, so should return 0 + // this previously returned 2 + $this->assertEquals( 0, $count ); + } }