Skip to content

[30691] Fix: Reset attachment post_parent when featured image is removed #8925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

Vedanshmini26
Copy link

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:

  1. 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

  2. set_post_thumbnail():
    Sets the _thumbnail_id meta field
    Updates the attachment's post_parent to establish a proper relationship

Copy link

github-actions bot commented Jun 5, 2025

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 sheldorofazeroth.

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

Copy link

github-actions bot commented Jun 5, 2025

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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • 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.

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.

1 participant