Home Forums General Issues Headless WordPress: ACF fields return empty once a post is created with WP-API


Headless WordPress: ACF fields return empty once a post is created with WP-API

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

    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.

    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.

  • I’m not sure that I can help you, but I can take a guess. ACF requires that each field as an associated field key reference in the database. Usually, if you must go to the admin and update to have changes appear it means that the field key reference is missing.

  • Thanks for the reply John. Through digging deeper, I’ve discovered that when creating a post that has ACF fields via the wp-api, for whatever reason none of those ACF fields are saved to the wp_postmeta table yet. It’s the “hard save” in the wp-admin panel that actually saves the ACF fields and stores them in the wp_postmeta table.

    I’ve tried to find a way to mimic that but have had no luck. If only there was a way to create a post that has ACF fields with the API, and then have those fields write to the wp_postmeta table upon creation. They just don’t save to the db until the “update” button is clicked πŸ™ƒ

    Which is incredibly unfortunate when trying to use WordPress only as a Headless CMS, with no wp-admin interaction except via the API.

  • Solved!!!

    I dug into the ACF core for a while and after a ton of trial and error, I found a super simple way to do this.

    I created a custom endpoint that does the following:

    // Get new post's ID
    $post_id = $request['id'];
    // Get new post's meta fields
    $meta_fields = get_post_meta($post_id);
    // Save the meta fields to the post 
    acf_save_post($post_id, $meta_fields);
  • Thanks for your update @speakincode! This might actually help me out with my problem as well πŸ™‚

  • Can you please explain better how did you solve that? Why did you create a custom endpoint? I’m using the default one of wordpress with acf rest api plugin but doesn’t work. Could you share your complete solution? Thanks in advance

  • Had the same issue, but for creating a new post using automated functions. So created this function that gets all the fields with their defaults values and updates the post metadata.

    Make sure you run this only after creating the new post and before adding data to it.

  • I think I may be having a related problem.

    I’m creating a bulk importer to take youtube videos and bring them as oembed fields in standard wordpress posts.

    I’ve done the process manually from the admin using a field group that I put together.

    When I attempt to use the REST API via a Python application and make my http post requests to create a new wordpress post, I get a new post but the ACF field group is null in the acf object.

    I was hoping that this was not a problem given that the recent updates to the REST API sound like this ought not to happen?

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Headless WordPress: ACF fields return empty once a post is created with WP-API’ is closed to new replies.