• 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)
  • 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();

Viewing 1 replies (of 1 total)
  • The topic ‘How can I programmatically insert a custom post with custom fields?’ is closed to new replies.