Post to WordPress using REST API from external site

To answer your broader question about resources – while there are many articles out there, I would always recommend checking out the official WordPress API Handbook first.

You first have to decide which JavaScript library you want to use to make HTTP requests to access the REST API. Axios is one example.

To set the featured image, you first have to uploaded it as media (which you can do via the REST API) and then include the media id in the post request (more details below).

Then to create the post via the REST API, you would make a POST HTTP request to https:/<your-site>/wp-json/wp/v2/posts with a body like:

{
    content: 'My post content here',
    title: 'Post Title',
    featured_media: [image-media-id],
    status: 'publish' // or 'draft'
}

To get the ACF Custom Fields accessible/editable via the REST API, check out this plugin: https://github.com/airesvsg/acf-to-rest-api/. I haven’t used it yet, but it seems like what you’re looking for.

Also be aware that if site ‘a’ is on a different domain than site ‘b’, you may have CORS issues to deal with as well. WordPress has good guides for dealing with that.

Leave a Comment