Ticket #4396 (new defect)

Opened 2 years ago

Last modified 1 year ago

Permalink leads to wrong attachment

Reported by: futurix Assigned to: anonymous
Priority: normal Milestone: 2.9
Component: General Version: 2.2
Severity: normal Keywords: wrong attachment permalink has-patch
Cc: futurix

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:
* First post: http://test/2007/06/03/first-post/one/
* Second post: http://test/2007/06/03/second-post/one-2/
* Third post: http://test/2007/06/03/third-post/one-2/
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

4396.diff (0.7 kB) - added by Nazgul on 07/21/07 16:05:12.

Change History

06/03/07 01:45:14 changed by futurix

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.

06/08/07 23:10:19 changed by rob1n

  • milestone changed from 2.2.1 to 2.2.2.

07/21/07 16:05:12 changed by Nazgul

  • attachment 4396.diff added.

07/21/07 16:05:51 changed by Nazgul

  • keywords changed from wrong attachment permalink to wrong attachment permalink has-patch.

Patch based on futurix's comment added.

08/09/07 19:12:54 changed by foolswisdom

  • milestone changed from 2.2.2 to 2.2.3.

08/29/07 18:00:20 changed by foolswisdom

  • milestone changed from 2.2.3 to 2.3.