I am creating a post with wp_insert_post/wp_update_post. The post should have a featured image. But to set a featured image I need the postID. The problem is I do not have any postID. What I am doing is. I create a "profile page" for every user. Therefore the user just updates his data in the backend. The post is created automatically out of the data.

I tied things like:

set_post_thumbnail( wp_insert_post($post), $thumbnail_id );

But is does not work. Surprisingly set_post_thumbnail( wp_update_post($post), $thumbnail_id ); works. But in case I want to create a pot the first time, I need wp_insert_post.

I can provide the full code on request. Any ideas?

Thanks and regards Nico

  • wp_insert_post and wp_update_post return the post id when you use it, so you can use the id for stuff. – Wyck Mar 13 '14 at 23:27

As far as I know you can't add the thumbnail in the first array you send to create the post, you'll need to grab the post ID after the wp_insert_post, then run update_post_meta after the first part of the function.

If you post your function here we might be able to help a little further.

  • But how is it done from wordpress itself? If I create a post in the Admin area I can set a featured image while creating the post. Perhaps I could just copy and modify this action..? – kn1g Mar 14 '14 at 10:50
  • In WordPress, an id is established the second you click add new post. If you post your function here we can help. – Oturia Mar 15 '14 at 3:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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