Hi. Trying to set the values of User fields and Post Object fields using wp_insert_post. This works great with standard text/textarea fields but I’m not connecting the dots on the object-based fields. I’ve tried using the IDs for User/Post Object but that doesn’t seem to work. My code is below.
The $data variables are all $_POST vars. I’m using WP functions to get IDs for $team_member_id and $company_id and passing those into the meta_input array. Again, all other standard text fields work great but not those two.
“team_member” and “dealer” are the slugs for the ACF fields.
$team_member_id = get_user_by('email', $data['DEMAIL']); $company_object = get_page_by_title($data['DEALERNAME'], OBJECT, 'company'); $company_id = $company_object->ID; $post_id = wp_insert_post(array ( 'ID' => $lead_id, 'post_type' => 'leads', 'post_title' => $data['email'], 'post_content' => '', 'meta_input' => array( 'email' => $data['email'], 'first_name' => $data['FNAME'], 'last_name' => $data['LNAME'], 'company' => $company_id, 'contact_name' => $data['CONTACTNAM'], 'team_member' => $team_member_id, ) ));
The problem is that when inserting values in this manor that the acf field key reference is not being added to the database. For each field there is an acf field key reference. This is explained briefly on this page https://www.advancedcustomfields.com/resources/update_field/
Updating for field name only works for basic text type fields but will not work with complex fields. The field key reference is required for ACF to understand what the field value represents.
You can insert the values the way you are doing as long as you also insert the field key reference. Let’s say (example) that the field key for “team_member” is “field_XXXXXX”
//... 'meta_input' => array( 'email' => $data['email'], 'first_name' => $data['FNAME'], 'last_name' => $data['LNAME'], 'company' => $company_id, 'contact_name' => $data['CONTACTNAM'], 'team_member' => $team_member_id, '_team_member' => 'field_XXXXXX' )
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!