I have multiple forms using generic fields to update the data in types text fields. The add post form cred_save_data call works as expected, but the edit post form cred_save_data call doesn't seem to be firing. The code for both is below. The call for form ID 338 is working as expected, the one for form ID 344 is not. They both reference the same fields and the same post type. Please advise:
//-------copy values from generic select to text field
add_action('cred_save_data', 'update_status',10,2);
function update_staus($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==344)
{
error_log('cred save data, form 344');
if (isset($_POST['unitstatusselect']))
{
// add it to saved post meta
update_post_meta($post_id, 'wpcf-unitstatus', $_POST['unitstatusselect'], true);
}
}
}
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==338)
{
error_log('cred save data, form 338');
if (isset($_POST['unitstatusselect']))
{
// add it to saved post meta
update_post_meta($post_id, 'wpcf-unitstatus', $_POST['unitstatusselect'], true);
}
}
}
Hello,
The action hook "cred_save_data" will be triggered after user submit the editing/creating post form, see our document:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
And since you are editing/creating the posts of same post type/custom field, you can merge both functions into one, for example:
add_action('cred_save_data', 'update_status',10,2);
function update_status($post_id, $form_data)
{
// if a specific form
$arr = array(344, 338);
if (in_array( $form_data['id'], $arr) )
{
//error_log('cred save data, form 344');
if (isset($_POST['unitstatusselect']))
{
// add it to saved post meta
update_post_meta($post_id, 'wpcf-unitstatus', $_POST['unitstatusselect'], true);
}
}
}
I tried your code and now neither of my forms is updating the unitstatus meta field. Please advise.
Is there another supporter available in a different time zone? I'm in GMT -7
Unfortunately, due to holiday issue, we have few supporters online, you can provide a test site with the same problem, fill below private message box with login details and FTP access, also point the problem page URL, and where I can edit your custom PHP codes, I need a live website to test and debug this issue, thanks
Thanks for the details, I have changed the custom codes in your theme file "functions.php", line 40 from:
function update_staus($post_id, $form_data)
To:
function update_unit_status($post_id, $form_data)
Please test again, check if it is fixed.
The add new post form seems to set the field value again now, but the edit post form is still not updating the unit-status field.
I have changed line 47 of your custom PHP codes from:
update_post_meta($post_id, 'wpcf-unitstatus', $_POST['unitstatusselect'], ture);
To:
update_post_meta($post_id, 'wpcf-unitstatus', $_POST['unitstatusselect']);
Please test again, check if it is fixed.
More help:
https://developer.wordpress.org/reference/functions/update_post_meta/