Title: How can I programmatically insert a custom post with custom fields?
Last modified: August 21, 2016

---

# How can I programmatically insert a custom post with custom fields?

 *  [Dazzathedrummer](https://wordpress.org/support/users/dazzathedrummer/)
 * (@dazzathedrummer)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/how-can-i-programmatically-insert-a-custom-post-with-custom-fields/)
 * Hi,
 * I’m working on a form that will update a database and create a post at the same
   time.
 * On successful submission to the DB I am implementing this code that I found on
   the net….
 *     ```
       global $user_ID;
       $new_post = array(
       'post_title' => 'My New Post',
       'post_content' => 'Lorem ipsum dolor sit amet...',
       'post_status' => 'publish',
       'post_date' => date('Y-m-d H:i:s'),
       'post_author' => $user_ID,
       'post_type' => 'post',
       'post_category' => array(0)
       );
       $post_id = wp_insert_post($new_post);
       ```
   
 * (with specific data)
 * The Theme that I am using has a custom post type ‘Event’ which contains custom
   fields of which I have identified one of them to be called ”snbp_event_venue’
   in the postmeta table.
 * How can I incorporate this field into the above code so I can update it?
 * I’ve tried adding it to the bottom of the array but I get an error……the ‘Event’
   is created but the custom field is not update.
 * Darren

Viewing 1 replies (of 1 total)

 *  [nick.mcelligott](https://wordpress.org/support/users/nickmcelligott/)
 * (@nickmcelligott)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/how-can-i-programmatically-insert-a-custom-post-with-custom-fields/#post-4059580)
 * You must insert the post and then update the ACFs.
 *     ```
       global $user_ID;
       $new_post = array(
       'post_title' => 'My New Post',
       'post_content' => 'Lorem ipsum dolor sit amet...',
       'post_status' => 'publish',
       'post_date' => date('Y-m-d H:i:s'),
       'post_author' => $user_ID,
       'post_type' => 'post',
       'post_category' => array(0)
       );
       $post_id = wp_insert_post($new_post);
   
       update_field('FIELD_KEY_NOT_NAME', $'snbp_event_venue_value, $post_id);
       ```
   
 * [See the ACF documentation on update_field();](http://www.advancedcustomfields.com/resources/functions/update_field/)

Viewing 1 replies (of 1 total)

The topic ‘How can I programmatically insert a custom post with custom fields?’ 
is closed to new replies.

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [field](https://wordpress.org/support/topic-tag/field/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [nick.mcelligott](https://wordpress.org/support/users/nickmcelligott/)
 * Last activity: [12 years, 5 months ago](https://wordpress.org/support/topic/how-can-i-programmatically-insert-a-custom-post-with-custom-fields/#post-4059580)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
