Introduce description__like arg to get_terms(). Make description__like and name__like perform LIKEs with a wildcard on both sides of passed string. Previously, strings had to match the beginning of the name, so searching for burrito in This is a burrito would fail. Adds unit tests.

Props aaroncampbell for the original patch, 5 years ago.
Fixes #8214.



git-svn-id: https://develop.svn.wordpress.org/trunk@25241 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor
2013-09-04 19:40:17 +00:00
parent d10b9d32a0
commit 89997e4c80
2 changed files with 40 additions and 3 deletions
+29
View File
@@ -150,4 +150,33 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
$terms = get_terms( 'post_tag', array( 'hide_empty' => false, 'search' => 'bur', 'fields' => 'ids' ) );
$this->assertEqualSets( array( $term_id1, $term_id2 ), $terms );
}
function test_get_terms_like() {
$term_id1 = $this->factory->tag->create( array( 'name' => 'burrito', 'description' => 'This is a burrito.' ) );
$term_id2 = $this->factory->tag->create( array( 'name' => 'taco', 'description' => 'Burning man.' ) );
$terms = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'bur', 'fields' => 'ids' ) );
$this->assertEqualSets( array( $term_id1 ), $terms );
$terms2 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => 'bur', 'fields' => 'ids' ) );
$this->assertEqualSets( array( $term_id1, $term_id2 ), $terms2 );
$terms3 = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'Bur', 'fields' => 'ids' ) );
$this->assertEqualSets( array( $term_id1 ), $terms3 );
$terms4 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => 'Bur', 'fields' => 'ids' ) );
$this->assertEqualSets( array( $term_id1, $term_id2 ), $terms4 );
$terms5 = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'ENCHILADA', 'fields' => 'ids' ) );
$this->assertEmpty( $terms5 );
$terms6 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => 'ENCHILADA', 'fields' => 'ids' ) );
$this->assertEmpty( $terms6 );
$terms7 = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'o', 'fields' => 'ids' ) );
$this->assertEqualSets( array( $term_id1, $term_id2 ), $terms7 );
$terms8 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => '.', 'fields' => 'ids' ) );
$this->assertEqualSets( array( $term_id1, $term_id2 ), $terms8 );
}
}