post_id missing from the wp-admin file upload request

I’m trying to upload an attachment using the “Add Media” button placed right above the editor.

The request goes to the wp-admin/async-upload.php file.
I can put a var_dump($_REQUEST); call there.

Whenever I upload an attachment for a regular post, the $_REQUEST contains the post_id key and the attachment has that post saved as it’s parent_post.

But when I do the same for a custom post type I’ve created, the post_id key is missing. I have no idea why.

Tried adding XHR breakpoints to that upload request, but they all lead to the minified plupload.full.min.js code that seems abstracted from whatever data is being passed through it. I tried searching for the entry point where it starts that upload (and probably has some condition on whether to include the post id) but couldn’t find it.

A hidden input with the correct post_id is present on both pages.

When I call media_handle_upload('file', $post_id); from elsewhere in the code, the file gets attached as expected.

Why do the custom post type upload requests omit the post_id? Am I missing something in the post type declaration?

Please help

2 Answers
2

Ok this seems to have fixed it:

function wp_plupload_include_attachment_id( $params ) { 
    global $post_ID; 
    if ( isset( $post_ID ) ) 
        $params['post_id'] = (int) $post_ID; 
    return $params; 
} 
add_filter( 'plupload_default_params', 'wp_plupload_include_attachment_id' ); 

This preprocesses the uploader $params and makes sure the post_id is included.

Taken from here: https://core.trac.wordpress.org/attachment/ticket/22085/media.php.patch

Leave a Comment