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:)
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
:
<?php
function wrestling_show_type()
{
register_post_type('wrestling',
array('labels' => array('name' => 'Wrestling Shows', 'singular_name' => 'Wrestling Show'),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'wrestling')));
flush_rewrite_rules();
}
add_action('init', 'wrestling_show_type');
?>
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:$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: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 whatwp_insert_post()
returns. So, you can do something like this:With the ID, then things are easy, just call
update_field()
for each field you want to update.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 useupdate_field()
instead, just like the other 3.There is a way to add custom fields data directly when creating a post. It is mentioned in the wp_insert_post() docs. Just pass an array with custom field keys and values to
meta_input
like this:I’m looking into this approach to avoid a dozen of update_field() database calls and make it less resource intensive, but I have yet to prove this approach actually makes less database calls in the background.