Skip to content

Rewrite & Republish copies not deleted when redirect is interrupted #419

@aozou99

Description

@aozou99
  • I've searched for any related issues and avoided creating a duplicate issue.

Please give us a description of what happened

When using the "Rewrite & Republish" feature, duplicate posts occasionally remain in the database with post_status = 'dp-rewrite-republish' instead of being deleted after the republishing process completes. These orphaned copies are not visible in the WordPress admin post list (by design, since show_in_admin_all_list is false), but they remain in the database indefinitely.

Key observations:

  • The republishing process itself succeeds - the original post is updated correctly with the new content
  • However, the cleanup process that should delete the duplicate copy does not execute
  • This appears to happen when the redirect after republishing is interrupted before the cleanup can run

Root cause analysis:
The plugin uses a two-stage HTTP request architecture:

  1. First request: Execute republish + send redirect response
  2. Second request: Load redirect destination + execute cleanup via clean_up_after_redirect()

The cleanup depends on the second request successfully loading, which can fail due to:

  • User closes browser/tab before redirect completes
  • Network interruption or timeout
  • Nonce expiration (24-hour default)
  • Session timeout

Database evidence:
We found 4 orphaned copies, all several months old, with their original posts successfully published:

SELECT
    p.ID as copy_id,
    p.post_title as copy_title,
    p.post_status as copy_status,
    pm_original.meta_value as original_id,
    orig.post_status as original_status
FROM wp_2_posts p
LEFT JOIN wp_2_postmeta pm_original ON p.ID = pm_original.post_id
    AND pm_original.meta_key = '_dp_original'
LEFT JOIN wp_2_posts orig ON pm_original.meta_value = orig.ID
WHERE p.post_status = 'dp-rewrite-republish'
copy_id copy_status original_id original_status post_modified
12447 dp-rewrite-republish 76 publish 2025-07-18 14:56:48
12318 dp-rewrite-republish 169 publish 2025-07-02 14:04:08
12236 dp-rewrite-republish 105 publish 2025-06-24 09:59:04
12141 dp-rewrite-republish 205 publish 2025-06-16 16:36:57

To Reproduce

Step-by-step reproduction instructions

This issue is difficult to reproduce consistently as it depends on timing and interruption. However, the following steps may trigger it:

Case 1: User interruption during redirect

  1. Create a "Rewrite & Republish" copy of a published post
  2. Edit the copy and click "Publish"
  3. Immediately after clicking publish (before the redirect completes):
    • Close the browser tab, OR
    • Navigate away from the page, OR
    • Interrupt the network connection
  4. Check the database for posts with post_status = 'dp-rewrite-republish'

Case 2: Long-running republish process (Most likely scenario)
This is particularly reproducible with posts that have many ACF (Advanced Custom Fields) fields:

  1. Create a post with many ACF fields (e.g., 20+ custom fields with repeaters, flexible content, etc.)
  2. Create a "Rewrite & Republish" copy
  3. Edit the copy and click "Publish"
  4. The republish process takes a long time to update all ACF meta fields (potentially 10-30+ seconds)
  5. During this waiting period, the user may:
    • Assume the process hung and close the tab
    • Navigate away thinking the save failed
    • Click browser back button
    • Close the browser
  6. The republish completes server-side, but the cleanup never runs because the redirect wasn't followed

Note: In our environment, all 4 orphaned copies were custom post types with extensive ACF field configurations, supporting this as the primary reproduction scenario.

Case 3: Nonce expiration

  1. Create a "Rewrite & Republish" copy
  2. Edit the copy and click "Publish"
  3. Wait more than 24 hours (or until nonce expires)
  4. The cleanup process will fail nonce verification

Expected results

  1. The duplicate copy should be deleted from the database after republishing completes
  2. The original post should be updated with the new content
  3. The _dp_has_rewrite_republish_copy meta should be removed from the original post
  4. No posts with post_status = 'dp-rewrite-republish' should remain in the database

Actual results

  1. The original post is updated correctly (republish succeeds)
  2. However, the duplicate copy remains in the database with post_status = 'dp-rewrite-republish'
  3. The _dp_has_rewrite_republish_copy meta may remain on the original post
  4. The orphaned copies are invisible in the admin UI but consume database space
  5. No error message is shown to the user

Screenshots, screen recording, code snippet

Relevant code sections:

The cleanup process in src/post-republisher.php:213-227:

public function clean_up_after_redirect() {
    if ( ! empty( $_GET['dprepublished'] ) && ! empty( $_GET['dpcopy'] ) && ! empty( $_GET['post'] ) ) {
        $copy_id = \intval( \wp_unslash( $_GET['dpcopy'] ) );
        $post_id = \intval( \wp_unslash( $_GET['post'] ) );

        \check_admin_referer( 'dp-republish', 'dpnonce' );

        if ( \intval( \get_post_meta( $copy_id, '_dp_has_been_republished', true ) ) === 1 ) {
            $this->delete_copy( $copy_id, $post_id );
        }
        else {
            \wp_die( \esc_html__( 'An error occurred while deleting the Rewrite & Republish copy.', 'duplicate-post' ) );
        }
    }
}

This cleanup is hooked to load-post.php and only runs if the redirect destination loads successfully with the correct URL parameters.

Technical info

  • If relevant, which editor is affected (or editors):
  • Classic Editor
  • Block Editor (not tested, but likely affected)
  • Gutenberg Editor (not tested, but likely affected)
  • Which browser is affected (or browsers):
  • Chrome
  • Firefox
  • Safari
  • Other: Browser-agnostic issue (architecture-level problem)

Used versions

  • Device you are using: Server environment (issue occurs server-side)
  • Operating system: Linux
  • PHP version: 8.2
  • WordPress version: 6.6.2
  • WordPress Theme: Custom theme
  • Yoast Duplicate Post version: 5.2.0
  • Classic Editor plugin version: 1.6.5
  • Relevant plugins in case of a bug: WordPress multi-site installation with custom post types, Advanced Custom Fields (ACF) Pro with extensive field configurations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions