2

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?

3
  • 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
    Commented Jan 19, 2012 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
    Commented Jan 19, 2012 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
    Commented Jan 20, 2012 at 8:56

1 Answer 1

1

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!
5
  • 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
    Commented Jan 20, 2012 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
    Commented Jan 20, 2012 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
    Commented Jan 20, 2012 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
    Commented Jan 20, 2012 at 9:38
  • Thanks, got it! I went to postmeta table straight, missed it :-)
    – Jenny
    Commented Jan 20, 2012 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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