Make WordPress Core

Opened 12 months ago

Last modified 2 months ago

#52422 new defect (bug)

Create a draft with the same slug as an existing post, the existing post will be 404.

Reported by: Toro_Unit Owned by:
Milestone: 6.0 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch has-unit-tests needs-refresh has-testing-info needs-testing
Focuses: Cc:


Step-by-step reproduction instructions

  1. create custom post type. and set 'show_in_rest' => true
function custom_init() {
        $args = array(
                'public' => true,
                'label'  => 'Books',
                'show_in_rest' => true,
        register_post_type( 'book', $args );
add_action( 'init', 'custom_init' );
  1. Publish new post of the post type.
  2. Create draft post of the post type with same slug.
  3. Go to the published post. but show draft post. if logged out, 404.

Using the block editor, a page can also create drafts with the same slug, which causes the same problem.

Attachments (2)

issue.gif (2.1 MB) - added by Toro_Unit 12 months ago.
A gif animation of the issue.
issue-on-page.gif (4.3 MB) - added by Toro_Unit 12 months ago.
reproduced on page.

Change History (11)

#1 @h2ham
12 months ago

It also happened in default posts.

  1. Set permalink to %postname%.
  2. Publish new post of default posts.
  3. Create draft post of the default post with same slug.
  4. Go to the published post. but show draft post. If logged out, 404.

#2 @peterwilsoncc
12 months ago

I've attempted to reproduce this on both 5.6 and trunk without success. I've tried with both a CPT per the original post and standard posts per the follow up comment.

Are either of you able to reproduce this with all plugins disabled and using a default theme such as Twenty Twenty-One?

#3 @Toro_Unit
12 months ago

@peterwilsoncc Thanks.

No plugins are used, and the theme is Twenty Twenty One. reproduced on WordPress 5.5 and 5.6.

When creating a post, please use the block editor.
Quick Edit or Classic Editor don't allow us to create a post with the same slug as an existing post.

12 months ago

A gif animation of the issue.

12 months ago

reproduced on page.

This ticket was mentioned in PR #1333 on WordPress/wordpress-develop by torounit.

8 months ago

  • Keywords has-patch has-unit-tests added; needs-patch removed

Trac ticket: https://core.trac.wordpress.org/ticket/52422.

Using the block editor, a draft can have the same slug as a post that already exists.

There is a part in WP_Query that uses get_page_by_path, so it may load the wrong post.

A hack similar to wp_ajax_inline_save, fixed so that the slug is different.


#5 @prbot
7 months ago

Shizumi commented on PR #1333:

I think the problem with this bug is not that it can be saved with the same slug, but that articles that are not published are called.

#6 @Toro_Unit
7 months ago

get_page_by_path is used in WP_Query.

  • Draft page ( id: 1, slug: foo, status: draft )
  • Published page ( id: 2, slug: foo, status: publish )

If the above two posts exist, get_page_by_path will get the draft page. That post will be set to queried_object.


#7 @SergeyBiryukov
7 months ago

  • Component changed from General to Posts, Post Types

#8 @SergeyBiryukov
7 months ago

  • Milestone changed from Awaiting Review to 5.9

#9 @hellofromTonya
2 months ago

  • Keywords needs-refresh has-testing-info needs-testing added
  • Milestone changed from 5.9 to 6.0

Updating the keywords:

  • Marking as needs-refresh as there are merge conflicts
  • Marking as has-testing-info as there are step-by-step instructions
  • Marking as needs-testing as I too am unable to reproduce the reported problem

I'm not able to reproduce this problem either. It needs more investigation. As 5.9 Beta 1 is in 2 days, moving it to 6.0.

Note: See TracTickets for help on using tickets.