WordPress.org

Make WordPress Core

Opened 22 months ago

Last modified 28 hours ago

#24612 reopened defect (bug)

Permalink conflict: image attachment page and post when permalink setting is /%postname%/

Reported by: augustas Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.3
Component: Permalinks Keywords: has-patch needs-unit-tests
Focuses: Cc:

Description

Here is scenario when there is a permalink conflict between the "attachment page" URL and post when image filename and post name are equal:
Permalinks are set to /%postname%/

Using Media -> Library -> upload new image called about-me.jpg
System saves into "wp_posts" database table: post_name = "about-me"

I publish a new POST with the title "About Me". For this post database stores the post_name = "about-me"

If I go to URL: www.example.com/about-me/ - I am redirected to about-me.jpg Image Attachment Page.

If POST is created first and the image uploaded second - URL will open the POST.

This issue is similar to #13459

Attachments (1)

24612.patch (1.2 KB) - added by SergeyBiryukov 14 months ago.

Download all attachments as: .zip

Change History (8)

comment:1 @augustas22 months ago

  • Summary changed from Permalink conflict when image is not attached to the post and permalink setting is /%postname%/ to Permalink conflict: image attachment page and post when permalink setting is /%postname%/

comment:2 @SergeyBiryukov22 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

comment:3 @SergeyBiryukov14 months ago

  • Milestone set to Awaiting Review
  • Resolution duplicate deleted
  • Status changed from closed to reopened

Reopening per ticket:15665:28, as #15665 only covered pages.

comment:4 @aaroncampbell14 months ago

I ran into this yesterday. I definitely think that posts should take precedence over attachments in this case. Especially since the UI to change media slugs is hidden in screen options by default.

@SergeyBiryukov14 months ago

comment:5 @SergeyBiryukov14 months ago

  • Keywords has-patch needs-unit-tests added
  • Version changed from 3.5.1 to 3.3

Introduced in [18541], specifically with the changes in WP_Rewrite::page_rewrite_rules().

24612.patch fixes it for me.

Last edited 14 months ago by SergeyBiryukov (previous) (diff)

comment:6 @aaroncampbell14 months ago

24612.patch works for me. It's very specific to "post" though. Do we want to assume that handling slug conflicts for custom post types should be done in the plugin that creates the post type?

Last edited 29 hours ago by SergeyBiryukov (previous) (diff)

comment:7 @SergeyBiryukov28 hours ago

A workaround:

function wp24612_resolve_post_vs_attachment_permalink_conflict( $query ) {
	if ( is_admin() || ! $query->is_main_query() ) {
		return;
	}

	if ( ! preg_match( "/^[^%]*%(?:postname)%/", get_option( 'permalink_structure' ) ) ) {
		return;
	}

	if ( empty( $query->queried_object ) || '' === $query->get( 'pagename' ) ) {
		return;
	}

	if ( 'attachment' === $query->queried_object->post_type ) {
		// See if we also have a post with the same slug
		$post = get_page_by_path( $query->get( 'pagename' ), OBJECT, 'post' );
		if ( $post ) {
			$query->queried_object    = $post;
			$query->queried_object_id = (int) $post->ID;
			$query->is_page           = false;
			$query->is_single         = true;
		}
	}
}
add_action( 'parse_query', 'wp24612_resolve_post_vs_attachment_permalink_conflict' );
Note: See TracTickets for help on using tickets.