Make WordPress Core

Opened 102 minutes ago

Last modified 73 minutes ago

#66851 new defect (bug)

sanitize_title() throws fatal error on array

Reported by: leedxw Owned by:
Priority: normal Milestone: Awaiting Review
Component: General Version: 7.1.5
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Similar to #66653 but not REST API related.

Related, but I have an issue with string operations on an array:

curl "http://localhost/?s=x&taxonomy=category&term[]=1"
PHP Fatal error:  Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in /var/www/html/wp-includes/formatting.php:1612
Stack trace:
#0 /var/www/html/wp-includes/formatting.php(1612): preg_match()
#1 /var/www/html/wp-includes/formatting.php(2232): remove_accents()
#2 [internal function]: sanitize_title()
#3 /var/www/html/wp-includes/class-wp-term-query.php(558): array_map()
#4 /var/www/html/wp-includes/class-wp-term-query.php(308): WP_Term_Query->get_terms()
#5 /var/www/html/wp-includes/class-wp-tax-query.php(644): WP_Term_Query->query()
#6 /var/www/html/wp-includes/class-wp-tax-query.php(567): WP_Tax_Query->transform_query()
#7 /var/www/html/wp-includes/class-wp-tax-query.php(395): WP_Tax_Query->clean_query()
#8 /var/www/html/wp-includes/class-wp-tax-query.php(325): WP_Tax_Query->get_sql_for_clause()
#9 /var/www/html/wp-includes/class-wp-tax-query.php(275): WP_Tax_Query->get_sql_for_query()
#10 /var/www/html/wp-includes/class-wp-tax-query.php(251): WP_Tax_Query->get_sql_clauses()
#11 /var/www/html/wp-includes/class-wp-query.php(2308): WP_Tax_Query->get_sql()
#12 /var/www/html/wp-includes/class-wp-query.php(3984): WP_Query->get_posts()
#13 /var/www/html/wp-includes/class-wp.php(704): WP_Query->query()
#14 /var/www/html/wp-includes/class-wp.php(825): WP->query_posts()
#15 /var/www/html/wp-includes/functions.php(1352): WP->main()
#16 /var/www/html/wp-blog-header.php(16): wp()
#17 /var/www/html/index.php(17): require('...')
#18 {main}
  thrown in /var/www/html/wp-includes/formatting.php on line 1612

I can test for a multidimensional array:

--- wp-includes/class-wp-term-query.php	2026-11-27 13:13:41.606148394 +0000
+++ wp-includes/class-wp-term-query.php.edit	2026-11-27 13:10:05.203603924 +0000
@@ -555,6 +555,13 @@
 		if ( '' === $args['slug'] ) {
 			$args['slug'] = array();
 		} else {
+			if (is_array($args['slug'])) {
+				foreach ($args['slug'] as $value) {
+					if (is_array($value)) {
+						error_log("DEBUG multidimensional array: ".json_encode($args['slug']));
+					}
+				}
+			}
 			$args['slug'] = array_map( 'sanitize_title', (array) $args['slug'] );
DEBUG multidimensional array: [["1"]]

Change History (2)

This ticket was mentioned in ​PR #16290 on ​WordPress/wordpress-develop by ​@abhishekfdd.


76 minutes ago
#1

  • Keywords has-patch has-unit-tests added

On PHP 8, a front-end request such as ?taxonomy=category&term[]=1 causes a fatal error:

TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given
in wp-includes/formatting.php (remove_accents)

Root cause

WP::parse_request() lets a one-level array through for public query vars, so term reaches WP_Query as ["1"]. WP_Query::parse_tax_query() then wraps it again ('terms' => array( $query_vars['term'] )), giving [["1"]], and WP_Term_Query::get_terms() runs sanitize_title() over the inner array. An array taxonomy (?taxonomy[]=category&term=1) fatals earlier, in WP_Tax_Query::sanitize_query() at line 165 (Cannot access offset of type array on array), because the value is used as an array key.

Fix

parse_tax_query() now builds the taxonomy/term clause only when taxonomy is a string and term is a scalar. is_scalar() keeps 'term' => 1 from PHP callers working. Malformed input behaves exactly like a missing term: the parameter is ignored. This matches #66653 / #15923, where a non-string namespace falls through to the existing "unknown" path, and the s, subpost and attachment_id precedents in WP_Query::parse_query(), which discard non-scalar values.

Why not treat a flat array as several terms? term has always meant one slug: get_query_var( 'term' ) and queried_terms assume one. Querying several terms already works through the taxonomy's own query var (?genre=a,b or ?genre[]=a).

Why no guard in WP_Term_Query? Dropping non-scalar slugs there could leave the slug list empty. An empty list adds no slug condition, so every term in the taxonomy is returned and the post query matches posts in any term. That turns a fatal into silently wrong results.

Testing

PHP 8.3.36, local wordpress-develop Docker environment, on trunk at d450653f30.

These requests fataled before the fix and return 200 after it (home, or search results for the first one):

  • ?s=x&taxonomy=category&term[]=1
  • ?taxonomy=category&term[]=1
  • ?taxonomy=category&term[a][]=1
  • ?taxonomy=category&term[]=
  • ?taxonomy=genre&term[]=x (custom public taxonomy)
  • ?taxonomy[]=category&term=1
  • ?taxonomy[]=category&term[]=1

?category_name[]=uncategorized, ?cat[]=1, ?tag[]=foo and ?genre[]=x did not fatal before and behave the same after.

New tests in tests/phpunit/tests/query/taxQuery.php (@ticket 66851):

  • test_non_scalar_taxonomy_or_term_query_var_is_ignored: new WP_Query(), 10 data sets.
  • test_request_with_non_scalar_taxonomy_or_term_query_var: go_to() with the query strings above.
  • test_scalar_term_query_var_is_used: a control test that a scalar term still filters.

Results:

  • npm run test:php -- --group 66851, before the fix: 17 tests, 15 errors (the reported TypeErrors), 1 failure. After the fix: OK (17 tests, 60 assertions).
  • npm run test:php -- --group query,taxonomy: 2547 tests, 8804 assertions, 3 skipped, no failures. There is 1 warning from the unrelated Tests_WP_Generate_Tag_Cloud::test_should_include_tag_link_position_class (a PHPUnit "Expecting E_DEPRECATED is deprecated" notice).
  • PHPCS (Core ruleset): no errors. The test file is clean, and class-wp-query.php has the same 40 warnings as on trunk.

Follow-ups (not in this PR)

  • Nested arrays in other taxonomy vars (?genre[a][]=x, ?category_name[a][]=x, ?tag[a][]=x, ?cat[a][]=1) don't fatal, but they log Array to string conversion warnings at class-wp.php:358, class-wp-query.php:862 and taxonomy.php:1136.
  • The publicly-queryable check in WP::parse_request() compares taxonomy with === and no is_string() check, so an array value isn't caught there. This PR makes that harmless, because parse_tax_query() now ignores an array taxonomy.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Root-cause tracing, the fix and the PHPUnit tests. I reviewed the changes and the test results.

#2 @abhishekfdd
73 minutes ago

Confirmed on trunk with PHP 8.3. The cause is in WP_Query::parse_tax_query(), not WP_Term_Query. WP::parse_request() lets a one-level array through for term, and parse_tax_query() then wraps it again ('terms' => array( $qterm )), so WP_Term_Query gets 1. An array taxonomy fatals earlier, in WP_Tax_Query::sanitize_query().

PR 16290 ignores non-scalar taxonomy and term values, so malformed input behaves like a missing term. That matches #66653 and how parse_query() already treats s, subpost and attachment_id arrays. Arrays never produced a working query here on any PHP version, so nothing that works today changes. The PR has unit tests for the reported URL and its variants.

Out of scope, and I'll open follow-ups if this direction is accepted: nested arrays in taxonomy query vars cause "Array to string conversion" warnings, and the publicly-queryable check in parse_request() doesn't verify that taxonomy is a string.

Note: See TracTickets for help on using tickets.