• Setup:

    • I have a Vue app that is using a separate WordPress install as a headless CMS.
    • “ACF to REST API” plugin in installed.
    • Custom Post-Type created with many ACF fields, some with default values.

    Question:
    Does anyone have a solution for when you create a post with the WP-API that has Advanced Custom Fields, and then having those custom fields returned in the response or when you run a GET request with the API?

    The fields do exist in the wp-admin once created with the API, but are not returned in the response. Unless you go into the admin area of WordPress and specifically click the “Update” button on that post. Only then do the ACF fields show up in the API response.

    Essentially what is happening is the ACF fields are not wrting to the post_meta table in the database until the “Update” button is manually clicked. Is there a way publish a posts meta fields to the db externally?

    Main Issue:
    When using WordPress as a headless CMS, I can’t very well manually click that button in the admin area. There must be a way to create a post via the API and have all the ACF fields returned without having to explicitly save it in the WP admin area…right? Otherwise, what’s the point of using WordPress as a headless CMS if you can’t.

    • This topic was modified 5 years, 3 months ago by speakincode6.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The issue lies with how ACF and WordPress handle saved data. As you’ve said, you need to click Publish/Update for the values to be saved to the database before they appear which makes perfect considering the flow of data inside WordPress.

    The ACF groups and fields themselves are all posts connected together with post meta. ACF displays these fields on the post edit screen via JavaScript and are technically not present in the database (even as blank fields) relating to your post until you click save which is when everything is mapped together.

    The best way forward would be to hook into the REST API and when saving/creating a post, manually (through the API) save each ACF field. Of course this would cause some complications if fields changed but due to have ACF works behind the scenes, I’m not sure how feasible it is unfortunately.

    Thread Starter speakincode6

    (@speakincode6)

    @danieltj Thank you for the response. For now what I’ve done, is created a JSON object that mirrors what the fields response is after it’s actually saved in the admin area, and I send that JSON to the “fields” key when creating the post, which essentially is populating and updating the ACF fields and it works now.

    It’s just unfortunate to have to create my data models twice, once as ACF fields and once as a JSON object to post to them upon creation. I was really hoping there’d be a way to “mimic” clicking that “Update” button and thus saving all the data to the DB at that point.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Headless WordPress: ACF fields return empty once a post is created with WP-API’ is closed to new replies.