Make WordPress Core

Opened 7 years ago

Last modified 6 years ago

#40588 new defect (bug)

Trashing and restoring a draft post replaces the slug

Reported by: tjnowell's profile TJNowell Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.5
Component: Posts, Post Types Keywords: needs-patch needs-unit-tests
Focuses: Cc:


Using the latest version of WordPress, if you create a draft post with a slug, lets say 'test-post', then trash it and restore it, the slug becomes
__trashed-xxx, where xxx is a number

Related to

Attachments (1)

Screen Shot 2017-04-27 at 21.37.09.png (146.8 KB) - added by TJNowell 7 years ago.

Download all attachments as: .zip

Change History (5)

#1 @Karimeo
7 years ago

I was able to recreate the issue. It seems to happen with drafts that were never published before. I think that it might come from the fact that when the post is trashed the actual slug is saved in a meta '_wp_desired_post_slug' so it gets restored after. However if the post was never published, the slug isn't set and an empty string is saved in '_wp_desired_post_slug'. Then when restoring, we have the following:

if ( 'trash' === $previous_status && 'trash' !== $post_status ) {
        $desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true );
        if ( $desired_post_slug ) {
                delete_post_meta( $post_ID, '_wp_desired_post_slug' );
                $post_name = $desired_post_slug;

With an empty string being saved in '_wp_desired_post_slug' the condition never evaluates to true and restores the slug.

#2 @netweb
7 years ago

  • Keywords needs-patch needs-unit-tests added
  • Version changed from 4.7.4 to 4.5

#3 @subrataemfluence
7 years ago

I could reproduce the issue in 4.7.4. I created two posts entitled "40588 Test" and "40588 Test 2" and saved them both as Draft. After trashing and restoring the permalinks changed to "__trashed" and "__trashed-2" respectively. Means this holds true for 4.7.4 as well and not specific to version 4.5.

However, there is always a chance of WordPress replacing slugs of trashed posts after restoration because it has to check whether the same permalink (which the restored post originally had) is already present with other posts. For a huge database it would be resource consuming to search the entire WP_Posts table and then come up with a relevant permalink based on its title. It is always a better option for the admin to edit the permalink once it is restored.

Nevertheless, the permalink could be replaced in a better fashion after restoration like post_id-restored (e.g. 786-restored) if suggesting one is not the best option.

When trashed WordPress is actually replacing the permalink to __trashed-xx, so it would not be very unlikely that when it is restored WordPress does the same thing for one more time to make it like I mentioned.

#4 @MegNael
6 years ago

The same issue is on WordPress 4.9.7

Note: See TracTickets for help on using tickets.