From c6de5dfec5e3acfedeb0a9aa3b916ce8ef9da731 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 28 Oct 2014 14:56:33 +0000 Subject: [PATCH] Allow 'slug' param of `get_terms()` to accept an array. Props jfarthing84, dlh. Fixes #23636. git-svn-id: https://develop.svn.wordpress.org/trunk@30042 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 11 +++++++--- tests/phpunit/tests/term/getTerms.php | 30 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index b8261dfeec..fffc05f986 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1594,7 +1594,7 @@ function get_term_to_edit( $id, $taxonomy ) { * @type string $fields Term fields to query for. Accepts 'all' (returns an array of * term objects), 'ids' or 'names' (returns an array of integers * or strings, respectively. Default 'all'. - * @type string $slug Slug to return term(s) for. Default empty. + * @type string|array $slug Optional. Slug(s) to return term(s) for. Default empty. * @type bool $hierarchical Whether to include terms that have non-empty descendants (even * if $hide_empty is set to true). Default true. * @type string $search Search criteria to match terms. Will be SQL-formatted with @@ -1804,8 +1804,13 @@ function get_terms( $taxonomies, $args = '' ) { } if ( ! empty( $args['slug'] ) ) { - $slug = sanitize_title( $args['slug'] ); - $where .= " AND t.slug = '$slug'"; + if ( is_array( $args['slug'] ) ) { + $slug = array_map( 'sanitize_title', $args['slug'] ); + $where .= " AND t.slug IN ('" . implode( "', '", $slug ) . "')"; + } else { + $slug = sanitize_title( $args['slug'] ); + $where .= " AND t.slug = '$slug'"; + } } if ( ! empty( $args['name__like'] ) ) { diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 2be4e8cb28..47780ebf2b 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -352,6 +352,36 @@ class Tests_Term_getTerms extends WP_UnitTestCase { add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 ); } + public function test_get_terms_by_slug() { + $t1 = $this->factory->tag->create( array( 'slug' => 'foo' ) ); + $t2 = $this->factory->tag->create( array( 'slug' => 'bar' ) ); + + $found = get_terms( 'post_tag', array( + 'hide_empty' => false, + 'fields' => 'ids', + 'slug' => 'foo', + ) ); + + $this->assertEquals( array( $t1 ), $found ); + } + + /** + * @ticket 23636 + */ + public function test_get_terms_by_multiple_slugs() { + $t1 = $this->factory->tag->create( array( 'slug' => 'foo' ) ); + $t2 = $this->factory->tag->create( array( 'slug' => 'bar' ) ); + $t3 = $this->factory->tag->create( array( 'slug' => 'barry' ) ); + + $found = get_terms( 'post_tag', array( + 'hide_empty' => false, + 'fields' => 'ids', + 'slug' => array( 'foo', 'barry' ) + ) ); + + $this->assertEquals( array( $t1, $t3 ), $found ); + } + public function test_get_terms_hierarchical_tax_hide_empty_false_fields_ids() { // Set up a clean taxonomy. $tax = 'hierarchical_fields';