Tell us what you are trying to do?
I populate the parent post title from the first and last name form fields, I do this from the following code I copied from a support answer on your site.
add_action('cred_save_data', 'my_custom_save_data_action', 10, 2);
function my_custom_save_data_action( $post_id, $form_data ){
// if a specific form
if ($form_data['id']==13154) {
if(!empty($_POST['wpcf-first-name'])){
$first_name = $_POST['wpcf-first-name'];
}
if(!empty($_POST['wpcf-last-name'])){
$last_name = $_POST['wpcf-last-name'];
}
$title = $first_name .' '. $last_name;
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}
However now I want to do a similar thing but with the child form, if I duplicate the above code and change the form id I seem to break the site.
how do I add this function to another form?
Thx
This code is a PHP Function and is added to the Themes functions.php.
It defines a function, which in PHP needs to be unique.
So, if you just copy it and insert it again, a fatal error of already declared function is thrown.
Each function in PHP must be unique, so you have to rename the function, and then change the ID of the Form it should apply to. As well you have to ensure the fields you call (wpcf-first-name, wpcf-last-name) are present in the Form.
Finally, if you want to update related post data (so, you edit with the form Post A but you want to Update related Post B) then you first need to get that post.
This would then be done with the Toolset API to get related posts, as described here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
Hope that helps to proceed!
Ok I get that,
How about populating the child post title with the parent post title, how would I go about doing that.
As it would give me the same result.
Thx
In Toolset Forms, you have 3 ShortCodes that allow you to get data from the related parent post when creating a Child Post with a Child Post Form.
https://toolset.com/forums/topic/using-parent-fields-in-a-cred-form-as-field-values/#post-502706
(Note, the Documentation linked there is not anymore holding these shortcodes explanations and I requested to know where they are now, and/or why they are missing)
If that does not work out for you, you can apply a custom code.
Important in any case is to have the post connected already, so this will anyway work only on either a "Create Child Post Link Form" or on an Edit Form.
We need to know the parent ID. This is crucial, so later you can get the Title from that post.
On what are you working on? An Edit, or Create post form, and is this a one to one relation or a many to many relationships?
Note that on Relationship Forms, no API can be applied (only on a post or user forms).
My issue is resolved now. Thank you!