• I have no problem retrieving field values with a get request, but I can’t write the fields when creating a post.

    An example array of post data sent:

    $post = array(
    “title” => “post title”,
    “type” => “my-cpt”,
    “status” => “publish”,
    “excerpt” => “post excerpt”,
    ‘acf’ => array(
    “my_field” => “my value”
    )
    );

    The post and all fields are created except for the ACF fields. What am I doing incorrectly?

    https://wordpress.org/plugins/acf-to-wp-api/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Same issue here. any ideas?

    You can only read acf datas with this plugin.

    Here is a simple solution for read write acf data:

    add_action( 'rest_api_init', 'register_rest_acf_field' );
    function register_rest_acf_field() {
      register_rest_field( 'post_type', 'acf_field_name', [
        'get_callback'    => read_acf_field',
        'update_callback' => update_acf_field',
        'schema'          => null,
      ] );
    }
    
    function read_acf_field( $object, $field_name, $request ) {
      return get_field( $field_name, $object[ 'id' ] );
    }
    
    function update_acf_field( $value, $object, $field_name ) {
      if ( ! $value || ! is_string( $value ) ) {
        return;
      }
    
      return update_field( $field_name, strip_tags( $value ), $object->ID );
    }

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been damaged by the forum’s parser.]

    Thread Starter Josh88

    (@josh88)

    Thanks, I ended up just using another plugin which supports writing ACF fields via the API.

    In general this plugin’s documentation is seriously lacking. I’m surprised it’s not mentioned anywhere that seemingly expected functionality like this isn’t possible.

    Having the same issue. Which plugin did you end up using @josh88 ?

    Thread Starter Josh88

    (@josh88)

    This plugin – https://wordpress.org/plugins/acf-to-rest-api/

    It has a different route just for ACF data. So if you’re creating a post, you have to send a request using the base API route, then get the newly created post ID and use that to send the request with the ACF route. Once I figured that out it worked perfectly.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Writing ACF fields when creating post’ is closed to new replies.