Wordpress: Using wp_insert_post() to fill custom post type fields Wordpress: Using wp_insert_post() to fill custom post type fields wordpress wordpress

Wordpress: Using wp_insert_post() to fill custom post type fields


If you have used ACF, you should use their API to interact with the fields. There's a method called update_field() that does exactly what you are looking for. This method takes 3 parameters:

update_field($field_key, $value, $post_id)

$field_key is an ID ACF gives to each field you create. This image, taken from their very own documentation, shows you how to get it:

How to get the field key

Edit: $field_key Will also accept the field name.

$value and $post_id are pretty straight forward, they represent the value you want to set the field with, and the post you are updating.

In your case, you should do something to retrieve this $post_id. Fortunately, that's what wp_insert_post() returns. So, you can do something like this:

$post_information = array(    //'promotion_name' => $_POST['name'],    'post_type' => 'wrestling');$postID = wp_insert_post( $post_information ); //here's the catch

With the ID, then things are easy, just call update_field() for each field you want to update.

update_field('whatever_field_key_for_venue_field', $_POST['venue'], $postID);update_field('whatever_field_key_for_main_event_field', $_POST['main_event'], $postID);update_field('whatever_field_key_for_fee_field', $_POST['fee'], $postID);

So basically what you're doing is creating the post first, and then updating it with the values.

I've done this kind of stuff in the functions.php file, and it worked fine. From what I've seen, I think you are using this routine in a template file of some sort. I think it's gonna work fine, you just gotta make sure the ACF plugin is activated.

EDIT:

I forgot the promotion_name field. I commented the line inside $post_information, as it's not going to work. You should use update_field() instead, just like the other 3.

update_field('whatever_field_key_for_promotion_name_field', $_POST['name'], $postID);