[wp-trac] Re: [WordPress Trac] #4396: Permalink leads to wrong attachment

WordPress Trac wp-trac at lists.automattic.com
Sun Jun 3 01:45:14 GMT 2007


#4396: Permalink leads to wrong attachment
----------------------------------------+-----------------------------------
 Reporter:  futurix                     |        Owner:  anonymous
     Type:  defect                      |       Status:  new      
 Priority:  normal                      |    Milestone:  2.2.1    
Component:  General                     |      Version:  2.2      
 Severity:  normal                      |   Resolution:           
 Keywords:  wrong attachment permalink  |  
----------------------------------------+-----------------------------------
Comment (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.

-- 
Ticket URL: <http://trac.wordpress.org/ticket/4396#comment:1>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list