Skip Navigation

[Resolved] ShortCode to get parent post data in Forms

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can I get data from a parent post in Toolset Forms?

Solution:
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 in above thread is not anymore holding these shortcodes explanations)

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 8.05 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by Waldo 5 years, 9 months ago.

Assisted by: Beda.

Author
Posts
#1203304

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

#1203606

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!

#1204036

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

#1204430

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).

#1208648

My issue is resolved now. Thank you!