Rewrite & Republish: images attached to the copy are not attached to the original post after republishing

Open
#572 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
php, wordpress
Domain
backend

Research direction

Start with Post_Republisher::delete_copy() in src/post-republisher.php and trace how the copy is removed; then review how the copy inherits its parent in src/post-duplicator.php. Confirm the reported behavior on WordPress 7.2 with a child page and an unattached image. Done means republishing leaves that image attached to the original page.

Written by the indexing model from the issue text.

Description

bug compatibility
  • I've searched for any related issues and avoided creating a duplicate issue.
Please give us a description of what happened

Images that get attached to a Rewrite & Republish copy are not attached to the original post after you republish.

WordPress 7.2 makes this more common. On a manual save in the block editor, WordPress 7.2 attaches every unattached image used in an Image block (also inside a Gallery) to the post being edited. See Media changes in WordPress 7.2, section "Unattached images auto-attach". When you edit a Rewrite & Republish copy, that post is the copy.

On republish, Post_Republisher::delete_copy() deletes the copy with wp_delete_post() (src/post-republisher.php). Nothing moves the copy's attachments to the original post first. wp_delete_post() then points the attachments "up one level", to the post_parent of the deleted copy. The copy inherits post_parent from the original (src/post-duplicator.php), so the images end up:

  • unattached, when the original has no parent;
  • attached to the parent page of the original, when the original is a child page.

Before WordPress 7.2 the same happens to images that you upload while editing the copy. WordPress 7.2 extends it to images picked from the Media Library.

This report comes from reading the code of Yoast Duplicate Post and WordPress. It has not been reproduced on a WordPress 7.2 site yet.

To Reproduce
Step-by-step reproduction instructions
  1. Use WordPress 7.2 with attachment pages disabled (the default for sites created on WordPress 6.4 or later).
  2. Upload an image in Media > Add New Media File, so that it is unattached.
  3. Create and publish a child page (a page with a parent page).
  4. Click "Rewrite & Republish" on that page.
  5. In the copy, add an Image block, pick the unattached image from the Media Library, and save the draft.
  6. In Media > Library (list mode), check the "Uploaded to" column: the image is attached to the copy.
  7. Republish the copy.
  8. Check the "Uploaded to" column again.
Expected results
  1. The image is attached to the original page.
Actual results
  1. The image is attached to the parent page of the original. For a top-level post or page, the image is unattached.
Screenshots, screen recording, code snippet

wp_delete_post() in wp-includes/post.php:

$parent_data  = array( 'post_parent' => $post->post_parent );
$parent_where = array( 'post_parent' => $post_id );
// ...
// Point all attachments to this post up one level.
$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );
Technical info
  • If relevant, which editor is affected (or editors):
  • Block Editor
  • Gutenberg Editor
  • Classic Editor
  • Other:
  • Which browser is affected (or browsers):
  • Chrome
  • Firefox
  • Safari
  • Other:
Used versions
  • Device you are using:
  • Operating system:
  • PHP version:
  • WordPress version: 7.2
  • WordPress Theme:
  • Yoast Duplicate Post version: trunk
  • Gutenberg plugin version:
  • Classic Editor plugin version:
  • Relevant plugins in case of a bug:
Dominant language
PHP
Stars
53
Forks
40
Avg merge
34m
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Yoast/duplicate-post

All issues in Yoast/duplicate-post

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.