Wordpress - Publish post with featured image at same time via PHP

146 views Asked by At

I am publishing to Wordpress via PHP, as follows.

  • Create post with wp_insert_post()
  • Add Featured Image with wp_insert_attachment()

Problem:

When creating the Post, there is a Plugin (SNAP autoposter) that will automatically publish it on Facebook. But, as obvious, it does not wait until the featured image is attached.

Question:

How should I proceed to publish the post with an already assigned attachment?

Thanks a lot for your help :)

1

There are 1 answers

0
Mathew Tinsley On BEST ANSWER

Don't publish the post until the attachment has been added.

  • Set the post_status to draft when you call wp_insert_post.
  • Add the attachment to the draft.
  • Use wp_publish_post to publish the post.

If the plugin is setup correctly, it will be listening for the status transition to publish.