WordPress.org

Make WordPress Core

Opened 6 months ago

Last modified 6 weeks 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: 5.9 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

Step-by-step reproduction instructions

  1. create custom post type. and set 'show_in_rest' => true
<?php
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 6 months ago.
A gif animation of the issue.
issue-on-page.gif (4.3 MB) - added by Toro_Unit 6 months ago.
reproduced on page.

Change History (10)

#1 @h2ham
6 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
6 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
6 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.

@Toro_Unit
6 months ago

A gif animation of the issue.

@Toro_Unit
6 months ago

reproduced on page.

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


8 weeks 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.

https://i0.wp.com/user-images.githubusercontent.com/1908815/120768076-c2bda580-c556-11eb-9b2b-88333c6e3af6.gif

#5 @prbot
6 weeks ago

Shizumi commented on PR #1333:

@torounit
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
6 weeks 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.

https://github.com/WordPress/wordpress-develop/blob/5.7.2/src/wp-includes/class-wp-query.php#L1001

#7 @SergeyBiryukov
6 weeks ago

  • Component changed from General to Posts, Post Types

#8 @SergeyBiryukov
6 weeks ago

  • Milestone changed from Awaiting Review to 5.9
Note: See TracTickets for help on using tickets.