Set ACF field value programmatically
-
I’m posting data to a custom post type over REST that has ACF fields. I have custom code that handles the REST request and creates the post. The post is created fine, shows up in the custom post area, but I can’t figure out how to set the ACF field values. I’ve tried using add_post_meta with both the field name and the field number, and none of these options work. I think I’m missing something very simple but I’ve been googling for 3 days with no luck. Here’s the code I’m using.
$data = file_get_contents('php://input'); $new_post = array( 'post_title' => 'My New Post', 'post_content' => $data, 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'custompost', 'post_category' => array(0), ); header("Access-Control-Allow-Origin: *"); header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header("Content-Type: application/json; charset=UTF-8"); // Create new post $post_id = wp_insert_post($new_post); // Update post with event data if (! is_wp_error($post_id)) { $data = json_decode(file_get_contents('php://input')); if (json_last_error() !== JSON_ERROR_NONE) { exit(json_last_error()); } // I've also tried the following function with the field name instead // of the ACF field number if (! add_post_meta($post_id, 'field_5e426305bbd97', 'hello world') ) { header("HTTP/1.1 406"); exit('406 (Error)'); } } // Send back response header("HTTP/1.1 200 OK"); exit($data['event-data']['delivery-status']['code']);//'200 (Success)');
- The topic ‘Set ACF field value programmatically’ is closed to new replies.