Make WordPress Core

Opened 17 years ago

Closed 15 years ago

#4396 closed defect (bug) (fixed)

Permalink leads to wrong attachment

Reported by: futurix's profile futurix Owned by:
Milestone: 2.8 Priority: normal
Severity: normal Version: 2.2
Component: Upload Keywords: has-patch commit
Focuses: Cc:

Description

Problem:
Some permalinks lead to wrong attachments.

My WordPress 2.2 configuration:

  • Date and time based permalinks
  • Uploads organized into month and year based folders

Way to reproduce the problem:

  • Write a new post, attach a new file with title of 'one', then publish the post
  • Write a second post, attach a new file with title of 'one', then publish the post
  • Write a third post, attach a new file with title of 'one', then publish the post

Resulting permalinks for the attachment:




Going to those links shows the correct file for the first and the third post. However the link for the second post shows the file of the third post.

All files are uploaded correctly, the problem is only in the link.

Attachments (1)

4396.diff (719 bytes) - added by Nazgul 17 years ago.

Download all attachments as: .zip

Change History (8)

#1 @futurix
17 years ago

Am I correct in my assumption, that the attachment slugs should be unique in WordPress?
If it is true, I can see an obvious flaw in wp_insert_attachment() function in /wp-includes/post.php:

Currently the unique name check looks like this:

$post_name_check =
	$wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'inherit' AND ID != '$post_ID' LIMIT 1");

if ($post_name_check) {
	$suffix = 2;
	while ($post_name_check) {
		$alt_post_name = $post_name . "-$suffix";
		$post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'inherit' AND ID != '$post_ID' AND post_parent = '$post_parent' LIMIT 1");
		$suffix++;
	}
	$post_name = $alt_post_name;
}

So the name check uses different SQL queries for the first check and for all subsequent ones. If indeed the attachment slugs should be globally unique, then the second query shouldn't have the post_parent check.

Like this:

$post_name_check =
	$wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'inherit' AND ID != '$post_ID' LIMIT 1");

if ($post_name_check) {
	$suffix = 2;
	while ($post_name_check) {
		$alt_post_name = $post_name . "-$suffix";
		$post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'inherit' AND ID != '$post_ID' LIMIT 1");
		$suffix++;
	}
	$post_name = $alt_post_name;
}

I tried the modification above, and the attachment slugs are unique now.

#2 @rob1n
17 years ago

  • Milestone changed from 2.2.1 to 2.2.2

@Nazgul
17 years ago

#3 @Nazgul
17 years ago

  • Keywords has-patch added

Patch based on futurix's comment added.

#4 @foolswisdom
17 years ago

  • Milestone changed from 2.2.2 to 2.2.3

#5 @foolswisdom
17 years ago

  • Milestone changed from 2.2.3 to 2.3

#6 @DD32
15 years ago

  • Component changed from General to Upload
  • Keywords commit added; wrong attachment permalink removed
  • Milestone changed from 2.9 to 2.8
  • Owner anonymous deleted

works for me under current trunk (2.8), all uploads get unique slugs, but it seems to only be because the post_title is passed to wp_insert_attachment() uniquely, upload "one.jpg" and the 2nd time the title will be "one1"

The original patch seems to be still be relevant, particularly for plugins which add attachments.

#7 @westi
15 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [10693]) Syncronise the queries used for attachment slug uniqueness checking. Fixes #4396 props futurix and Nazgul.

Note: See TracTickets for help on using tickets.