[wp-trac] [WordPress Trac] #29646: wp_insert_attachment() doesn't allow to add post_parent for orphan attachments

WordPress Trac noreply at wordpress.org
Sun Sep 14 10:34:38 UTC 2014


#29646: wp_insert_attachment() doesn't allow to add post_parent for orphan
attachments
----------------------------+--------------------
 Reporter:  dikiy_forester  |       Owner:
     Type:  defect (bug)    |      Status:  new
 Priority:  normal          |   Milestone:  4.0.1
Component:  Media           |     Version:  4.0
 Severity:  normal          |  Resolution:
 Keywords:                  |     Focuses:
----------------------------+--------------------

Comment (by dikiy_forester):

 Sorry, my explanation was not clear, please look the code below, hope it's
 better:

 {{{
 #!php
 // get the some orphan attachment
 $attachment = get_post( $attachment_id );

 // ! Important $attachment->'post_parent' = 0

 // then try to insert attachment to some post with ID 999 for ex.
 $attach_id = wp_insert_attachment( $attachment, '', $post_id = 999 );

 // now look to the WP4.0 wp_insert_attachment() code

 $defaults = array(
         'file'        => $file,
         'post_parent' => $parent
 );

 // where $parent is passed $post_id value
 // and $args is passed $attachment array/object

 // then function is merging two arrays
 $data = wp_parse_args( $args, $defaults );

 // $args['post_parent'] = 0
 // $defaults['post_parent'] = 999

 // Result:
 // $data['post_parent'] = 0
 // but not 999 as expected.

 // attachment is still be orphan
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/29646#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list