• Hi all,
    maybe the subject is quite complicated but the idea is very simple.
    I’ve got a file in uploads. I just want to attach it to a post.
    I’m looking for which function I can make it with.
    I know there must be something like that, because in Media I can attach files manually. Unfortunatelly I tried finding the function with no result. It should be something like:
    attach($post_id, $attachment_id);
    I’d be grateful if somebody could help me.

    I need a function for deattaching. If someone knows it, please, let me know as well. If I have one of them I’ll find or write (if there isn’t built-in function) the second one by myself.

    Regards,
    Bernard

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter blatan

    (@blatan)

    I analyzed the DB and I noticed the attaching file is simply setting post_parent field in the row of the attachment.

    I’ve read I can’t set 2 parents for the post. Bad news for me.

    I wanted to attach one image to several posts. I think I can resolve it in other way. I can set custom field with the name of the file or simply with attachment_id.

    What do you think about it? Does anybody know any smarter ways to do it?

    I am very new to WordPress but I expect you can find what you want by following through what media_handle_upload() does in “wp-admin/includes/media.php”.

    Ultimately, you have to do the following to create the attachment:

    // Save the data
    	$id = wp_insert_attachment($attachment, $file, $post_id);
    	if ( !is_wp_error($id) ) {
    		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
    	}

    Setting up the attachment array looks relatively straight forward since it is pulling mostly out the $_POST array which in your case would be replaced with the known values for the preset file you want to attach.

    Cheers

    Thread Starter blatan

    (@blatan)

    Hi codegrunt,
    you’re right.
    wp_insert_attachment sets all what I need, but you have to add as the first argument an array. It isn’t necessery in my case, because my attachment has been already loaded to the DB. All I have to do is setting post_porent. If you want to dettach, you have to set post_parent to 0 (zero).

    But as I wrote in the second post – I can set only one parent of the attachment since the type of post_parent field is BIGINT. This way I answered by myself for my question from the first post and I stopped searching a proper function, but it’s very simple, even if you are to set it with $wpdb directly.

    My second question is if you know a good way to set many parents of one attachment. I think the best way would be setting custom_fields. You just has to use:
    add_post_meta($post_id, $meta_key, $meta_value, $unique);

    where the last optional argument $unique with default false allows you to set many meta values for one meta key. For instance:

    attached_image => image1.jpg
    attached_image => image2.jpg

    Then you can simply get it with:
    get_post_custom_values($key, $post_id);

    IMHO it’s the simplest way to set many images to many posts.

    I’d just ask you to confirm it or tell me some smarter way if you know any.

    PS.
    wp_insert_attachment is good when you want to post ($_POST) your form.
    I use media_sideload_image($file, $post_id, $desc);, where file is simply URL, because I load images from another server. It’s a very convient function.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘function for attaching existing attachment to a post’ is closed to new replies.