Opened 11 years ago
Closed 10 years ago
#29646 closed defect (bug) (fixed)
wp_insert_attachment() doesn't allow to add post_parent for orphan attachments
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 4.0.1 | Priority: | normal |
Severity: | normal | Version: | 4.0 |
Component: | Media | Keywords: | has-patch commit fixed-major |
Focuses: | Cc: |
Description
If you try to insert orphan attachment with post_parent=0 to the post and pass parameter $parent, it won't has effect, because 'post_parent' from $args array won't be replaced with another one from $defaults array.
function wp_insert_attachment( $args, $file = false, $parent = 0 ) { $defaults = array( 'file' => $file, 'post_parent' => $parent ); $data = wp_parse_args( $args, $defaults ); $data['post_type'] = 'attachment'; return wp_insert_post( $data ); }
In the WP3.9.2 it was treated with following code:
if ( !empty($parent) ) $object['post_parent'] = $parent;
Attachments (2)
Change History (12)
#1
@
11 years ago
- Component changed from General to Posts, Post Types
- Milestone changed from Awaiting Review to 4.0.1
#3
follow-up:
↓ 7
@
11 years ago
Sorry, my explanation was not clear, please look the code below, hope it's better:
// 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
#4
follow-up:
↓ 5
@
11 years ago
Problem caused by 28579 when wp_insert_attachment()
was made as a wrapper to wp_insert_post()
in ticket 21963.
Patch 29646.diff hardcodes the values of $defaults
and assigns the value of $parent
to $data['post_parent']
.
It also includes a unit test which
- Creates an attachment
- Asserts that the attachment is an orphan
- Creates a post
- Sets this post as the parent of the attachment
- Asserts that post_parent of the attachment has changed
#5
in reply to:
↑ 4
;
follow-up:
↓ 6
@
11 years ago
Replying to jesin:
Patch doesn't allows to process $file parameter. It always will false
.
#6
in reply to:
↑ 5
@
11 years ago
Replying to dikiy_forester:
Patch doesn't allows to process $file parameter. It always will
false
.
Patch 29646.2.diff changes only the post_parent
value of $defaults
.
#7
in reply to:
↑ 3
@
11 years ago
- Keywords has-patch commit added
Replying to dikiy_forester:
Sorry, my explanation was not clear, please look the code below, hope it's better
It is, thank you.
#8
@
11 years ago
- Owner set to SergeyBiryukov
- Resolution set to fixed
- Status changed from new to closed
In 29745:
Sounds like a regression. Could you provide a piece of code to reproduce the issue?