Home » Php » php – WordPress: Using wp_insert_post() to fill custom post type fields

php – WordPress: Using wp_insert_post() to fill custom post type fields

Posted by: admin July 12, 2020 Leave a comment


I have created a custom post type wrestling and created its corresponding custom fields using Advanced Custom Fields. Now, I wanted the users to fill this custom form on the front end, so that on submission, the data would get automatically updated in the custom post type in the dashboard. For this purpose, I created a custom page and assigned a custom template to it which contained the required form. There are four HTML form fields that the users are supposed to fill, named name, venue, main_event and fee respectively.

The custom form fields that I created using Advanced Custom Fields are named as promotion_name, venue, main_event_ and price respectively. Now, in order to fill the data entered by the users on the front end onto the custom post type fields at the dashboard, I tried using the wp_insert_post() function as follows:

$post_information = array(
        'promotion_name' => $_POST['name'],
        'venue' => $_POST['venue'],
        'main_event_' => $_POST['main_event'],
        'price' => $_POST['fee'],
        'post_type' => 'wrestling',

    wp_insert_post( $post_information );

However, after the user submits the form, a new entry (no_title) does appear in my custom post type, but the custom form fields are still empty (See images below:)

enter image description here

enter image description here

I’m sure this is because I’m not using the wp_insert_post() correctly for updating custom post types. I’d really appreciate some help here. Thanks.

PS: This is how I have defined my custom post type in functions.php:

function wrestling_show_type()
                    array('labels' => array('name' => 'Wrestling Shows', 'singular_name' => 'Wrestling Show'),
                        'public' => true,
                        'has_archive' => true,
                        'rewrite' => array('slug' => 'wrestling')));

add_action('init', 'wrestling_show_type');
How to&Answers:

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.


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