Skip to content

Commit c57aa56

Browse files
committed
Query: Prevent fatal TypeError when taxonomy query var is an array.
When a URL contains array-style GET parameters for a hierarchical taxonomy (e.g. ?category_name[]=foo), wp_basename() receives an array instead of a string, causing a fatal TypeError on PHP 8+. Added is_string() check before calling wp_basename() on the taxonomy query var value. The existing is_array() check on the next line already handles array values correctly, so this change only prevents the crash. Adds a unit test to verify the query completes without a TypeError when a hierarchical taxonomy query var is passed as an array. Props patricedefago. Fixes #64870.
1 parent dea662b commit c57aa56

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/wp-includes/class-wp-query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ public function parse_tax_query( &$query_vars ) {
11891189
'field' => 'slug',
11901190
);
11911191

1192-
if ( ! empty( $t->rewrite['hierarchical'] ) ) {
1192+
if ( is_string( $query_vars[ $t->query_var ] ) && ! empty( $t->rewrite['hierarchical'] ) ) {
11931193
$query_vars[ $t->query_var ] = wp_basename( $query_vars[ $t->query_var ] );
11941194
}
11951195

tests/phpunit/tests/query/parseQuery.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,33 @@ public function test_parse_query_attachment_id_nonscalar() {
233233

234234
$this->assertEmpty( $q->query_vars['attachment_id'] );
235235
}
236+
237+
/**
238+
* Ensure an array value for a hierarchical taxonomy query var
239+
* does not cause a fatal TypeError in wp_basename().
240+
*
241+
* @ticket 64870
242+
*/
243+
public function test_parse_query_hierarchical_taxonomy_query_var_array() {
244+
register_taxonomy(
245+
'wptests_tax',
246+
'post',
247+
array(
248+
'query_var' => 'wptests_tax',
249+
'rewrite' => array( 'hierarchical' => true ),
250+
'public' => true,
251+
)
252+
);
253+
254+
$q = new WP_Query(
255+
array(
256+
'wptests_tax' => array( 'term-a', 'term-b' ),
257+
)
258+
);
259+
260+
// The query should complete without a fatal TypeError.
261+
$this->assertIsArray( $q->posts );
262+
263+
unregister_taxonomy( 'wptests_tax' );
264+
}
236265
}

0 commit comments

Comments
 (0)