0
votes

Im setting up an endpoint that I want to use to update ACF fields in a custom post type.

Here I register the field endpoint and callback

function wp_api_assign_external_writer() {
  register_rest_route( 'responsify/v1', 'assign_external_writer/', array(
        'methods' => 'POST',
        'callback' => 'assign_external_writer_callback',
    ));
}
function assign_external_writer_callback( $request ) {


$parameters = $request->get_params();
$project_id = $parameters['project_id'];
$writer = $parameters['writer'];

update_field('content_external_writer',$writer,$project_id);

  
}

sending this request in postman: http://localhost:32851/wp-json/responsify/v1/assign_external_writer?writer=32272&project=2527

If I use "title=my_new_title" instead of writer it updates correctly. But using an ACF doesnt update. I read that this should work with update_field but it is not. Am I doing something wrong?

2

2 Answers

1
votes

It looks like you have the variable $project_id defined, but then in your update_field() call you are using a different variable name: $project

update_field('content_external_writer', $writer, $project);

If you change your update_field() call to use $project_id for the $post_id parameter, the update should work:

update_field('content_external_writer', $writer, $project_id);
0
votes

I notice using get_post_meta that a non-nested key for the metafield exists - 'project_type_content_content_external_writer'

so using

seems to work just fine