-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Taxonomy: Add 's' as an alias of 'search' in WP_Term_Query #11696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,7 @@ class WP_Term_Query { | |
| * @since 5.1.0 Introduced the 'meta_compare_key' parameter. | ||
| * @since 5.3.0 Introduced the 'meta_type_key' parameter. | ||
| * @since 6.4.0 Introduced the 'cache_results' parameter. | ||
| * @since 7.2.0 Introduced the 's' parameter as an alias of 'search'. | ||
| * | ||
| * @param string|array $query { | ||
| * Optional. Array or query string of term query parameters. Default empty. | ||
|
|
@@ -158,6 +159,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 | ||
|
|
@@ -211,6 +213,7 @@ public function __construct( $query = '' ) { | |
| 'term_taxonomy_id' => '', | ||
| 'hierarchical' => true, | ||
| 'search' => '', | ||
| 's' => '', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Comment by Claude Opus 5 Adding 1. 2. Duplicate cache entries for equivalent queries. Unsetting |
||
| 'name__like' => '', | ||
| 'description__like' => '', | ||
| 'pad_counts' => false, | ||
|
|
@@ -261,6 +264,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']; | ||
| } | ||
|
Comment on lines
+267
to
+270
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Comment by Claude Opus 5 The alias is resolved here, but the documented extension points run afterward and neither of them re-resolves it: Once add_action(
'pre_get_terms',
static function ( $query ) {
$query->query_vars['s'] = $_GET['s'];
}
);Setting |
||
|
|
||
| $query['number'] = absint( $query['number'] ); | ||
| $query['offset'] = absint( $query['offset'] ); | ||
|
|
||
|
|
@@ -425,6 +433,11 @@ public function get_terms() { | |
| */ | ||
| $args = apply_filters( 'get_terms_args', $args, $taxonomies ); | ||
|
|
||
| // Re-resolve 's' alias in case it was modified in a 'pre_get_terms' callback or 'get_terms_args' filter. | ||
| if ( '' === $args['search'] && '' !== $args['s'] ) { | ||
| $args['search'] = $args['s']; | ||
| } | ||
|
|
||
| // Avoid the query if the queried parent/child_of term has no descendants. | ||
| $child_of = $args['child_of']; | ||
| $parent = $args['parent']; | ||
|
|
@@ -1182,7 +1195,7 @@ protected function generate_cache_key( array $args, $sql ) { | |
| // $args can be anything. Only use the args defined in defaults to compute the key. | ||
| $cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ); | ||
|
|
||
| unset( $cache_args['cache_results'], $cache_args['update_term_meta_cache'] ); | ||
| unset( $cache_args['cache_results'], $cache_args['update_term_meta_cache'], $cache_args['s'] ); | ||
|
|
||
| if ( 'count' !== $args['fields'] && 'all_with_object_id' !== $args['fields'] ) { | ||
| $cache_args['fields'] = 'all'; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1252,4 +1252,162 @@ public function test_id_and_parent_fields_should_return_parent_ids_keyed_by_term | |
| $q2 = new WP_Term_Query(); | ||
| $this->assertSame( $expected, $q2->query( $query_args ), 'Second query is not of the expected form.' ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 51811 | ||
| * | ||
| * @covers WP_Term_Query::parse_query | ||
| */ | ||
| 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' ); | ||
|
|
||
| self::factory()->term->create( | ||
| array( | ||
| 'taxonomy' => 'wptests_tax', | ||
| 'name' => 'Existing Non Matching Term', | ||
| ) | ||
| ); | ||
|
|
||
| $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.' ); | ||
| } | ||
|
Comment on lines
+1316
to
+1336
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Comment by Claude Opus 5 This test passes whether or not the feature works. For the assertion to carry weight, a term whose name does not match |
||
|
|
||
| /** | ||
| * @ticket 51811 | ||
| * | ||
| * @covers WP_Term_Query::get_terms | ||
| */ | ||
| public function test_s_param_can_be_set_in_pre_get_terms() { | ||
| register_taxonomy( 'wptests_tax', 'post' ); | ||
|
|
||
| $term1 = self::factory()->term->create( | ||
| array( | ||
| 'taxonomy' => 'wptests_tax', | ||
| 'name' => 'Apple Term', | ||
| ) | ||
| ); | ||
| self::factory()->term->create( | ||
| array( | ||
| 'taxonomy' => 'wptests_tax', | ||
| 'name' => 'Banana Term', | ||
| ) | ||
| ); | ||
|
|
||
| $callback = static function ( $query ) { | ||
| $query->query_vars['s'] = 'Apple'; | ||
| }; | ||
|
|
||
| add_action( 'pre_get_terms', $callback ); | ||
|
|
||
| $q = new WP_Term_Query( | ||
| array( | ||
| 'taxonomy' => 'wptests_tax', | ||
| 'hide_empty' => false, | ||
| 'fields' => 'ids', | ||
| ) | ||
| ); | ||
|
|
||
| remove_action( 'pre_get_terms', $callback ); | ||
|
|
||
| $this->assertSame( array( $term1 ), $q->terms, "Setting 's' in pre_get_terms should filter terms." ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 51811 | ||
| * | ||
| * @covers WP_Term_Query::generate_cache_key | ||
| */ | ||
| public function test_s_and_search_produce_identical_cache_key() { | ||
| register_taxonomy( 'wptests_tax', 'post' ); | ||
|
|
||
| $q1 = new WP_Term_Query(); | ||
| $q1->query_vars = array( | ||
| 'taxonomy' => 'wptests_tax', | ||
| 'search' => 'Match', | ||
| 'cache_results' => true, | ||
| ); | ||
| $q1->parse_query( $q1->query_vars ); | ||
|
|
||
| $reflection = new ReflectionMethod( $q1, 'generate_cache_key' ); | ||
| if ( PHP_VERSION_ID < 80100 ) { | ||
| $reflection->setAccessible( true ); | ||
| } | ||
|
|
||
| $key1 = $reflection->invoke( $q1, $q1->query_vars, 'SELECT * FROM wp_terms' ); | ||
|
|
||
| $q2 = new WP_Term_Query(); | ||
| $q2->query_vars = array( | ||
| 'taxonomy' => 'wptests_tax', | ||
| 's' => 'Match', | ||
| 'cache_results' => true, | ||
| ); | ||
| $q2->parse_query( $q2->query_vars ); | ||
|
|
||
| $key2 = $reflection->invoke( $q2, $q2->query_vars, 'SELECT * FROM wp_terms' ); | ||
|
|
||
| $this->assertSame( $key1, $key2, 'Queries using search and s should produce identical cache keys.' ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
@sincefor new variable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 99ea3e2: added
@since 7.1.0 Introduced the 's' parameter as an alias of 'search'.to the constructor docblock, plus the@type string $sentry in the parameter list. Apologies for the delayed reply, the fix has been on the branch since May 10 but I never pinged the thread.