diff --git a/packages/editor/src/utils/media-upload/index.js b/packages/editor/src/utils/media-upload/index.js index 3edd4fec51d4b..f1f60b7c1e99a 100644 --- a/packages/editor/src/utils/media-upload/index.js +++ b/packages/editor/src/utils/media-upload/index.js @@ -31,17 +31,24 @@ export default function mediaUpload( { onError = noop, onFileChange, } ) { - const { getCurrentPostId, getEditorSettings } = select( editorStore ); + const { getCurrentPost, getEditorSettings } = select( editorStore ); const wpAllowedMimeTypes = getEditorSettings().allowedMimeTypes; maxUploadFileSize = maxUploadFileSize || getEditorSettings().maxUploadFileSize; - const currentPostId = getCurrentPostId(); + const currentPost = getCurrentPost(); + // Templates and template parts' numerical ID is stored in `wp_id`. + const currentPostId = + typeof currentPost?.id === 'number' + ? currentPost.id + : currentPost?.wp_id; + const postData = currentPostId ? { post: currentPostId } : {}; + uploadMedia( { allowedTypes, filesList, onFileChange, additionalData: { - ...( ! isNaN( currentPostId ) ? { post: currentPostId } : {} ), + ...postData, ...additionalData, }, maxUploadFileSize,