Title: Set ACF field value programmatically
Last modified: February 15, 2020

---

# Set ACF field value programmatically

 *  [prowse](https://wordpress.org/support/users/prowse/)
 * (@prowse)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/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)');
       ```
   
    -  This topic was modified 6 years, 3 months ago by [prowse](https://wordpress.org/support/users/prowse/).

The topic ‘Set ACF field value programmatically’ is closed to new replies.

 * ![](https://ps.w.org/advanced-custom-fields/assets/icon.svg?rev=3207824)
 * [Advanced Custom Fields (ACF®)](https://wordpress.org/plugins/advanced-custom-fields/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/advanced-custom-fields/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/advanced-custom-fields/)
 * [Active Topics](https://wordpress.org/support/plugin/advanced-custom-fields/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/advanced-custom-fields/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/advanced-custom-fields/reviews/)

## Tags

 * [add_post_meta](https://wordpress.org/support/topic-tag/add_post_meta/)
 * [api](https://wordpress.org/support/topic-tag/api/)
 * [rest](https://wordpress.org/support/topic-tag/rest/)

 * 0 replies
 * 1 participant
 * Last reply from: [prowse](https://wordpress.org/support/users/prowse/)
 * Last activity: [6 years, 3 months ago](https://wordpress.org/support/topic/set-acf-field-value-programmatically/)
 * Status: not resolved