title: How to attach media files to a post programmatically in WordPress?
body: <p>In WordPress, if a media file is uploaded within the WP admin post edit screen, it will automatically be attached to the current post being edited. However, I'm allowing my site's authors to upload images via the front-end. The upload form I have created is on the post page (within the loop) so I have the post's ID available when uploading the image.</p>
  
  <p>Is there a way to automatically attach the uploaded image to the post? Currently when I visit the <em>Media</em> section in WP admin, all of the images uploaded thus far are marked as 'unattached'.</p>
  
  <p>Ref:</p>
  
  <p><a href="https://codex.wordpress.org/Using_Image_and_File_Attachments#Attachment_to_a_Post" rel="noreferrer">https://codex.wordpress.org/Using_Image_and_File_Attachments#Attachment_to_a_Post</a></p>
  
  <p><strong>Edit:</strong> I should mention I'm using WordPress Liveblog: <a href="https://github.com/Automattic/liveblog" rel="noreferrer">https://github.com/Automattic/liveblog</a> - images are uploaded when a user pastes an image URL into the form.</p>