Skip to content

Taxonomy: Allow searching terms containing ampersands and HTML entities - #14187

Open
robelsust wants to merge 2 commits into
WordPress:trunkfrom
robelsust:fix/66279-category-search-ampersand
Open

robelsust wants to merge 2 commits into
WordPress:trunkfrom
robelsust:fix/66279-category-search-ampersand

Conversation

@robelsust

Copy link
Copy Markdown

Taxonomy: Allow searching terms containing ampersands and HTML entities

Trac ticket: https://core.trac.wordpress.org/ticket/66279


Description

When term names are saved in WordPress, _wp_specialchars() encodes ampersands as & into wp_terms.name (priority 30 on pre_term_name in default-filters.php).

Prior to this change, WP_Term_Query::get_search_sql() created search clauses using only the raw $search value:

((t.name LIKE '%search%') OR (t.slug LIKE '%search%'))

Because MySQL does not equate raw '&' with '&', searching for terms containing ampersands (such as "Tour & ", "Tour & Travel", or "& Travel") failed completely on both wp-admin/edit-tags.php and any queries powered by WP_Term_Query / get_terms( array( 'search' => ... ) ).

Solution

In WP_Term_Query::get_search_sql():

  1. Check if the search term contains characters requiring HTML entity encoding (_wp_specialchars( $search, ENT_COMPAT, 'UTF-8', true )).
  2. Decode pre-encoded entities (wp_specialchars_decode()) to support searches submitted with entities.
  3. If distinct variants are found, expand the search query with an OR condition across both forms:
    (((t.name LIKE '%Tour & %') OR (t.slug LIKE '%Tour & %')) OR ((t.name LIKE '%Tour & %') OR (t.slug LIKE '%Tour & %')))
  4. If no special characters are detected, a single clause is generated, preserving identical execution and indexing performance for standard terms.

Testing Instructions

  1. Create a category named Tour & Travel.
  2. Search for Tour & (with a trailing space) on Posts -> Categories (wp-admin/edit-tags.php?taxonomy=category).
  3. Verify that Tour & Travel is found in the search results table.
  4. Run PHPUnit test suite:
    vendor/bin/phpunit tests/phpunit/tests/term/getSearchSql.php
    vendor/bin/phpunit -c tests/phpunit/multisite.xml tests/phpunit/tests/term/getSearchSql.php
  5. Run PHPCS:
    vendor/bin/phpcs src/wp-includes/class-wp-term-query.php tests/phpunit/tests/term/getSearchSql.php

Fixes an issue where searching for terms containing ampersands (e.g. 'Tour & ' or 'Tour & Travel') failed to return matches because term names are stored with HTML entities in the database ('Tour & Travel') via _wp_specialchars().

WP_Term_Query::get_search_sql() now expands the search clauses to query both raw and HTML-entity-encoded forms when special characters are detected, ensuring consistent matches across partial and exact searches while preserving identical single-clause performance for standard terms.

Adds dedicated unit tests covering ampersands with trailing spaces, encoded entities, and standard term searches.

See #66279.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props therssoftware.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

…ity in getSearchSql tests.

In PHP 8.1+, reflection-accessed methods are accessible by default, and calling setAccessible() is deprecated in PHP 8.5. Guarding the call prevents deprecation errors when running tests on PHP 8.5+.

See #66279.

@SainathPoojary SainathPoojary left a comment

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.

Thanks for working on this. This fixes server-side searches containing a raw ampersand.

I also checked the reported issue in the block editor. Searching for "Tour & " still hides the category with this PR applied.

The reported block editor issue still occurs because Gutenberg filters category names in JavaScript. That part needs a separate fix in the Gutenberg.

$searches[] = $encoded;
}

$decoded = wp_specialchars_decode( $search, ENT_COMPAT );

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.

There is one remaining case with encoded searches. Both Tour & Travel and Tour & Travel return no results for the existing category. Decoding produces Tour & Travel, but the stored name is Tour & Travel. Including the encoded form of the decoded value, with tests for these inputs, would cover this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants