Make WordPress Core

Opened 2 years ago

Last modified 4 weeks ago

#62461 new defect (bug)

If a post has 2 term tags children's and kid+ and from all terms we have children's, kid and kid+, when the post/tags is saved, kid+ is saved as kid

Reported by: frankakot1 Owned by:
Priority: normal Milestone: Awaiting Review
Component: Posts, Post Types Version: 6.6
Severity: normal Keywords: has-screenshots has-test-info has-patch has-unit-tests
Cc: Focuses:

Description

There is an array compare for tags in wp-includes/post.php on line 4989 that will fail comparing the tags because children's will be escape as children\'s and as such the array comparison will be false, thus attaching tags to the post will be qureried by tag name on not buy slug, thus kid+ will be closer to kid, where the slug for kid+ is kid-plus while that for kid is kid.

Change History (6)

#1 @abcd95
2 years ago

  • Keywords has-screenshots has-testing-info added

Hey @frankakot1, Welcome to Trac, and thanks for bringing this up.

I was able to reproduce this. Here is the report:

This is a bug specific to WordPress 6.6.x where tags with special characters are incorrectly matched during post save.

Steps to Reproduce:

  1. Install WordPress 6.6
  2. Create three tags:
    • "kid+" (becomes slug: kid-plus)
    • "kid" (becomes slug: kid)
    • "children's" (becomes slug: childrens)
  3. Create a new post
  4. Add tags "kid+" and "children's"
  5. Save post
  6. Check tags - "kid+" becomes "kid"

Expected: The post should keep "kid+" and "children's" tags
Actual: "kid+" is saved as "kid"

However, this has been fixed in the later versions after 6.6.

Screenshots

https://postimg.cc/tZHsNL41
https://postimg.cc/Fdqn6gwJ

#2 @wordpressdotorg
18 months ago

  • Keywords has-test-info added; has-testing-info removed

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


4 weeks ago
#3

  • Keywords has-patch has-unit-tests added; needs-patch removed

wp_update_post() slashes the post array before comparing tags_input against the post's existing tag names. A tag name containing a character that wp_slash() escapes, such as an apostrophe in children's, no longer matches, so tags_input is not discarded and all of the names are passed to wp_set_post_tags(). There, term_exists() can resolve a name like kid+ to a different term whose slug is kid, so a title-only update silently swaps the post's tags.

This also affects the block editor: editing only a post's title sends a REST update without tags, which goes through wp_update_post() and triggers the same swap.

This PR unslashes tags_input before the strict comparison. It also adds a test that creates kid, kid+ and children's tags, assigns kid+ and children's to a post, updates only the title, and asserts that the tags are unchanged. The test fails without the fix (kid+ is replaced by kid) and passes with it.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude (claude.ai) and Claude Code
Model(s): Claude Opus 5.5
Used for: Claude was used to reproduce and diagnose the bug and to draft the fix and the test. Claude Code applied the patch, ran the tests and lint, and committed. I reviewed and tested the changes.

#4 @abhishekfdd
4 weeks ago

Reproduction Report

Description

This report validates that the issue can be reproduced and is still present on trunk. It also explains why comment:1 saw it as fixed. A patch with a unit test is in ​https://github.com/WordPress/wordpress-develop/pull/14469.

Environment

  • WordPress: 6.6 and 7.2-alpha (trunk at 88912df)
  • PHP: 8.3
  • Database: MariaDB 10.11 and SQLite
  • Theme/Plugins: defaults only

Steps

  1. Create tag kid first (gets slug kid).
  2. Create tag kid+ (gets slug kid-2; also reproduces with a manual slug of kid-plus).
  3. Create tag children's.
  4. Assign kid+ and children's to a post.
  5. Update only the post title, via wp_update_post() or in the block editor.

Actual Results

  • wp_update_post() title-only update: ❌ kid+ is replaced by kid on both 6.6 and trunk.
  • Block editor / REST title-only update: ❌ reproduces.
  • REST update that sends tag IDs: ✅ fine.
  • Classic editor (edit_post() with tax_input): ✅ fine.
  • Same post without the children's tag: ✅ fine.

Cause

In wp_update_post(), the post array from get_post( ARRAY_A ), including tags_input names, is passed through wp_slash(). children's becomes children\'s, so the strict comparison with the names from get_the_terms() fails and tags_input is not discarded. The names then go to wp_set_post_tags(), where term_exists( 'kid+' ) matches by sanitize_title( 'kid+' ), which is kid, and returns the wrong term.

Why comment:1 saw it as fixed

It depends on term creation order. If kid+ is created before kid, kid+ takes the slug kid itself and the slug lookup returns the right term. Creating kid first reproduces it on trunk every time. Also note that core does not generate the slug kid-plus on its own, so the original report likely involved a manually set slug or a plugin.

Patch

The PR unslashes tags_input before the comparison:

-               if ( $postarr['tags_input'] === $tag_names ) {
+               if ( wp_unslash( $postarr['tags_input'] ) === $tag_names ) {

It adds Tests_Post_wpInsertPost::test_wp_update_post_should_not_change_tags_with_slashable_names, which fails on trunk without the fix and passes with it. The post, taxonomy and term test groups pass with the change.

Additional Notes

This removes the trigger only. term_exists() matching a name against a sanitized slug is a separate problem: calling wp_set_post_tags( $id, 'kid+' ) directly still attaches kid. That may deserve its own ticket rather than being handled here.

AI assistance (Claude) was used to reproduce and diagnose the issue and draft the patch; I reviewed and tested it.

#5 @abhishekfdd
4 weeks ago

  • Keywords changes-requested removed

​@nathanrice commented on ​PR #14469:


4 weeks ago
#6

Verified the problem, and this looks like a clean and harmless fix. Tested it locally, and everything looks good.

Note: See TracTickets for help on using tickets.