WPIntell

Source evidence

Writing ACF fields when creating post

ACF to WP-API · support · 2016-03-11T06:34:00+00:00

complaintsentiment
mediumseverity
0.86relevance
5replies
Evidence linked to opportunitycommercial context

Proof Health

Open evidence

Commercial opportunities need traceable source links before they are treated as build-worthy.

5 / 31 rows with source links

16.1% of this page's analysis has direct source links.

0 build-decision rows missing links

0 rows here require auditable proof before promotion.

26 rows with no attached evidence

0 rows have source counts but still need direct links.

Conversation

support
Josh88 unresolved
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/ 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.] 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 ? 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.

Comments

5 shown
cb49747 2016-03-22T15:01:00+00:00

Same issue here. any ideas?

fischadiadi 2016-03-23T14:40:00+00:00

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

Josh88 2016-03-23T15:26:00+00:00

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.

mantismamita 2016-05-14T11:09:00+00:00

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

Josh88 2016-05-18T07:18:00+00:00

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.