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:
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);
Comments
-
Manas Chaturvedi almost 2 years
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, namedname
,venue
,main_event
andfee
respectively.The custom form fields that I created using Advanced Custom Fields are named as
promotion_name
,venue
,main_event_
andprice
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 thewp_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'); ?>
-
Manas Chaturvedi over 8 yearsI managed to update my form fields using the
add_post_meta()
function, which I think is exactly similar to theupdate_field()
function, right? -
Caio Felipe Pereira over 8 yearsI think it ultimately does that, but I'd stick with the plugin API, as its more consistent to a field created by the plugin. If you have different kinds of fields, such as WYSIWYG editors, or maybe subfields, using
add_post_meta()
can become quite tricky, so why not let the plugin do the job for you? -
Manas Chaturvedi over 8 yearsOh alright, I'll try to modify my code, this time using the
update_field()
method instead. Thanks. -
Caio Felipe Pereira over 8 yearsyea lemme know if you find any trouble
-
Jiro Matchonson over 5 yearsHi, but if you develop on localhost and then go publish to production, production will have different keys for the same fields ? So then 'update_field' would crash ?
-
Caio Felipe Pereira over 5 yearsAlthough this is pretty old and it may have changed since then, I think the keys would be the same. If not, you can update the database with whatever keys you want, or create production specific files to deal with the ID. Again, this answer is old and ACF probably has changed since them. I'd suggest some testing
-
Caio Felipe Pereira over 5 yearsAnd also, by the magic of reading what I wrote three years ago, I found out that
$field_key
also takes the Field Name, so you don't need to worry about keys change, since your names won't