Adding post and its attachments in once..
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..
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..
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_postyou 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.
- The topic ‘Adding post and its attachments in once..’ is closed to new replies.