Introduce `description__like` arg to `get_terms()`. Make `description__like` and `name__like` perform `LIKE`s 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

View File

@ -1166,9 +1166,12 @@ function get_term_to_edit( $id, $taxonomy ) {
* search - Returned terms' names will contain the value of 'search',
* case-insensitive. Default is an empty string.
*
* name__like - Returned terms' names will begin with the value of 'name__like',
* name__like - Returned terms' names will contain the value of 'name__like',
* case-insensitive. Default is empty string.
*
* description__like - Returned terms' descriptions will contain the value of
* 'description__like', case-insensitive. Default is empty string.
*
* The argument 'pad_counts', if set to true will include the quantity of a term's
* children in the quantity of each term's "count" object variable.
*
@ -1222,7 +1225,7 @@ function get_terms($taxonomies, $args = '') {
$defaults = array('orderby' => 'name', 'order' => 'ASC',
'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '',
'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '',
'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
$args = wp_parse_args( $args, $defaults );
$args['number'] = absint( $args['number'] );
@ -1345,7 +1348,12 @@ function get_terms($taxonomies, $args = '') {
if ( !empty($name__like) ) {
$name__like = like_escape( $name__like );
$where .= $wpdb->prepare( " AND t.name LIKE %s", $name__like . '%' );
$where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $name__like . '%' );
}
if ( ! empty( $description__like ) ) {
$description__like = like_escape( $description__like );
$where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $description__like . '%' );
}
if ( '' !== $parent ) {

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 );
}
}