When save post, I made dir in wp_uploads_dir, and copied 2 files and one folder in this dir. Now I want to do the WP_insert_attatchment process to make the 2 files as attachements. How can I do it?

Edit: I inset_attatchement by directly using their URL. Now they show up in Media Libray, but I can't see whether the attachment_id is recorded or not. Which table in database can I go to find it?

  • 2
    I doubt if you can do that. An attachment is created when you use the uploader to add files. Manually added files may be used with the actual URLs but I'm not aware if you can add them as attachment through WP. :-| – Sterex Jan 19 '12 at 10:30
  • I'm trying to use the URLs, haven't got it working yet. Not sure if it's my code wrong or it's wp reject. – Jenny Jan 19 '12 at 10:38
  • success. it can be added by URL. I can see the files in Media Library. Only problem is -- I can't find attachment_id in any dababase tables. – Jenny Jan 20 '12 at 8:56

You need the local path to add an attachment:

// add the file to the media library
$attachment = array(
    'post_mime_type' => 'image/png' // the MIME type
,   'post_title'     => 'Attachment title'

// Adds the file to the media library and generates the thumbnails.
$attach_id = wp_insert_attachment( $attachment, $path ); // PATH!
  • I added and edited my question. Now the problem is -- I want to comfirm that I added it at correct place. I want to see which database table the attachment_id is stored. – Jenny Jan 20 '12 at 9:15
  • Attachments are stored in the regular posts table, they are just a post type. wp_get_attachment_image( $attach_id ); will return an image element and wp_get_attachment_image_src( $attach_id ); the URL. – fuxia Jan 20 '12 at 9:25
  • I don't need the id. I just want to see where it is in the database. I checked in phpadmin, I don't see anywhere in the database there's a key about attachment. Which table should I look for it? – Jenny Jan 20 '12 at 9:30
  • The posts table. There is a column named post_type. Attachment are of the post type … well … attachment. :) Try SELECT * FROM `wp_posts` WHERE post_type = 'attachment' to get the attachments only. – fuxia Jan 20 '12 at 9:38
  • Thanks, got it! I went to postmeta table straight, missed it :-) – Jenny Jan 20 '12 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.