WordPress.org

Ready to get started?Download WordPress

Forums

Adding post and its attachments in once.. (5 posts)

  1. 5yn74x
    Member
    Posted 2 years ago #

    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..

  2. s_ha_dum
    Member
    Posted 2 years ago #

  3. 5yn74x
    Member
    Posted 2 years ago #

    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.

  4. s_ha_dum
    Member
    Posted 2 years ago #

    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

  5. 5yn74x
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic