WordPress.org

Support

Support » Plugins and Hacks » Adding post and its attachments in once..

Adding post and its attachments in once..

  • Hey..

    i have an automated app to add posts to my wp site..

    some of the posts have attachments(images) next to it and im supposed to add one as featured image, and the rest as images at the end of the content..

    the problem is, i want images to be attached to the post id, and since i upload the images in “wp_insert_post_data” Hook, i can’t get the post ID since it’s not created yet..

    yet, i don’t want to make another loop to add the attachments to the posts, i want one loop to add both post and its attachment..

    is there any hook/filter to edit the content applied inside wp_insert_post but after it got its new ID?

    thanks in advance..

Viewing 4 replies - 1 through 4 (of 4 total)
  • after exploring wp_insert_post code..

    i saw “$wpdb->insert_id” which is supposed to give the function the next id available..

    when i tested it, it gives me an id with ±2 offset, i guess due to drafts or something?

    the problem is save_post is called after the data added to the database, any editing at that point is considered as an extra work for me..

    thanks s_ha_dum, i’ll consider it if there is no other solution..

    Edit:
    insert_id in wp_insert_post is used after inserting the post (after wp_insert_post_data) so it won’t help that much to me.

    Your specification was that you “don’t want to make another loop to add the attachments to the posts, i want one loop to add both post and its attachment”. With save_post you shouldn’t have to create another loop.

    Anyway, you have to insert the post before it has an ID. If you want to do it with actions/filters that is it. You can call wp_insert_post or wp_update_post directly though. That will return your post ID for use when adding the attachments.

    http://codex.wordpress.org/Function_Reference/wp_insert_post

    thanks sir, i guess this is what will happen..

    wp_insert_post to add a post with just the title and some other information, and another wp_update_post afterward to set the content with the attachments..

    thanks again sir.. and have a great day.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding post and its attachments in once..’ is closed to new replies.
Skip to toolbar