Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/wp-includes/class-wp-term-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class WP_Term_Query {
* (even if `$hide_empty` is set to true). Default true.
* @type string $search Search criteria to match terms. Will be SQL-formatted with
* wildcards before and after. Default empty.
* @type string $s Alias of `$search`. Default empty.
* @type string $name__like Retrieve terms with criteria by which a term is LIKE
* `$name__like`. Default empty.
* @type string $description__like Retrieve terms where the description is LIKE
Expand Down Expand Up @@ -211,6 +212,7 @@ public function __construct( $query = '' ) {
'term_taxonomy_id' => '',
'hierarchical' => true,
'search' => '',
's' => '',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add @since for new variable.

'name__like' => '',
'description__like' => '',
'pad_counts' => false,
Expand Down Expand Up @@ -261,6 +263,11 @@ public function parse_query( $query = '' ) {

$query = wp_parse_args( $query, $this->query_var_defaults );

// 's' is a shorthand alias for 'search', matching WP_Query convention.
if ( '' === $query['search'] && '' !== $query['s'] ) {
$query['search'] = $query['s'];
}

$query['number'] = absint( $query['number'] );
$query['offset'] = absint( $query['offset'] );

Expand Down
72 changes: 72 additions & 0 deletions tests/phpunit/tests/term/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1207,4 +1207,76 @@ public function test_query_does_not_have_leading_whitespace() {

$this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
}

/**
* @ticket 51811
* @covers WP_Term_Query::parse_query
Comment on lines +1212 to +1213
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @ticket 51811
* @covers WP_Term_Query::parse_query
* @ticket 51811
*
* @covers WP_Term_Query::parse_query

Update other tests as well

*/
public function test_s_param_is_alias_for_search() {
register_taxonomy( 'wptests_tax', 'post' );

$term = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => 'Unique Findable Term',
)
);

$q = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax',
's' => 'Unique Findable',
'hide_empty' => false,
'fields' => 'ids',
)
);

$this->assertContains( $term, $q->terms, 'The s parameter should work as an alias for search.' );
}

/**
* @ticket 51811
* @covers WP_Term_Query::parse_query
*/
public function test_search_takes_precedence_over_s() {
register_taxonomy( 'wptests_tax', 'post' );

$term = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => 'Precedence Test Term',
)
);

$q = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax',
'search' => 'Precedence Test',
's' => 'nonexistent',
'hide_empty' => false,
'fields' => 'ids',
)
);

$this->assertContains( $term, $q->terms, 'The search parameter should take precedence over s.' );
}

/**
* @ticket 51811
* @covers WP_Term_Query::parse_query
*/
public function test_s_param_returns_empty_when_no_match() {
register_taxonomy( 'wptests_tax', 'post' );

$q = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax',
's' => 'absolutelynonexistentterm',
'hide_empty' => false,
'fields' => 'ids',
)
);

$this->assertEmpty( $q->terms, 'The s parameter should return empty for non-matching terms.' );
}
}
Loading