[30691] Fix: Reset attachment post_parent when featured image is removed#8925
[30691] Fix: Reset attachment post_parent when featured image is removed#8925Vedanshmini26 wants to merge 3 commits intoWordPress:trunkfrom
Conversation
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
trac: https://core.trac.wordpress.org/ticket/30691
Problem
When a featured image is removed from a post, WordPress only deletes the _thumbnail_id meta field but doesn't reset the attachment's post_parent field back to 0. This creates orphaned relationships where attachments still reference deleted parent posts, causing incorrect results when using functions like get_children() and get_posts().
For Example:
Set image ID 123 as the featured image for post ID 456
Remove the featured image from post 456
Image 123 still has post_parent = 456 in wp_posts table
get_children(456) incorrectly returns image 123 as a child
Root Cause
The delete_post_thumbnail() function in wp-includes/post.php only removes the post meta. Additionally, set_post_thumbnail() doesn't properly establish the post_parent relationship when setting a featured image.
Solution
This PR enhances both functions to properly manage the post_parent relationship:
delete_post_thumbnail():
Retrieves the current thumbnail ID before deletion
Removes the _thumbnail_id meta field
Resets the attachment's post_parent to 0 if it was set to the current post
set_post_thumbnail():
Sets the _thumbnail_id meta field
Updates the attachment's post_parent to establish a proper relationship