Skip Navigation

[Resolved] Save a post relationship parent title as a custom field entry in the child post

This support ticket is created 5 years, 1 month 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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 5 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1403431
Screen Shot 2019-12-09 at 21.47.50.png

Tell us what you are trying to do?

I have a toolset form that generates a CPT (equipment), with custom fields consisting of model, year, serial number etc. You also set the post relationship parent (manufacturers-equipments) at the same time. The 'manufacturers' are in a one to many relationship with the equipment. This all works as expected so far.

In addition to this I would like that the CPT parent that is selected in the form, 'manufacturers-equipments' post title to be saved within the 'equipments' CPT custom fields, as 'equipment-manufacturer' for example, so that i can call on that information in a gravity form that requires post meta from the 'equipment' CPT.

Is there any documentation that you are following?

Not for this part, i cant find anything.

Is there a similar example that we can see?

Not sure.

What is the link to your site?

hidden link (its a private admin site)

#1404099

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The child posts are published using a Toolset Form which includes a selector for the parent post, and you want the title of the parent post to be saved in some custom field of the child post when the form is submitted, correct?

For that you would need to use the Forms API, a hook such as cred_save_data.

You should be able to identify the ID of the parent post in the $_POST object, and can retrieve the post title using get_post() and save it with update_post_meta(), as required.

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/get_post/
https://developer.wordpress.org/reference/functions/update_post_meta/

If you get stuck, let me know.

#1408171

Hi Nigel,

Thanks for your input, i managed to also find some help/code from another couple of support threads, and then used this code which works for what I need it to do. One quick question, if i need to to run it for multiple forms, I have just duplicated it in this instance, but could I instead just list the forms with a comma between them?

add_action('cred_submit_complete', 'ts_update_post_field_with_parent_post_title',10,2);

function ts_update_post_field_with_parent_post_title($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==61)
{
$relationship = 'manufacturer-equipment';//The slug of the relationship you want to target
$manufacturer_id = toolset_get_related_post($post_id, $relationship ); //to get the post id of the related manufacturer
$manufacturer_name = get_the_title( $manufacturer_id ); //to get the post title of the manufacturer specified by the post id

//Example of what to do with such data
update_post_meta($post_id, 'wpcf-equipment-make', $manufacturer_name);

}

// if a specific form
if ($form_data['id']==1292)
{
$relationship = 'manufacturer-equipment';//The slug of the relationship you want to target
$manufacturer_id = toolset_get_related_post($post_id, $relationship ); //to get the post id of the related manufacturer
$manufacturer_name = get_the_title( $manufacturer_id ); //to get the post title of the manufacturer specified by the post id

//Example of what to do with such data
update_post_meta($post_id, 'wpcf-equipment-make', $manufacturer_name);

}
}

#1408415

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The way to run the same code snippet for multiple forms would be to make an array of the form IDs and use in_array, like so:

    $target_forms = array( '123', '456', '789' );
    if ( in_array( $form_data['id'], $target_forms ) )