Skip Navigation

[Resolved] Copy address field from parent CPT to address field of child CPT

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

Problem:

Customer wants that the address of the parent CPT aktivitaten get copied into the address field of the child CPT bewertung.
He saw an example using _wpcf_belongs_ but it is not working.

Solution:

Since Types version 3.x the usage of _wpcf_belongs_{parent-post-type-slug}_id has been discontinued. Now you need to use toolset_get_related_post when there can be only one result, e.g. you are starting with the ID of a child post and you want to get its parent (there can be only one parent, for a given relationship).

Or, you would use toolset_get_related_posts when there could be multiple results, e.g. you are starting with the ID of a parent post and you want to get all of the IDs of all of the child posts for some relationship.

See
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

and

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 5 replies, has 2 voices.

Last updated by Mateus Getulio 9 months ago.

Assisted by: Mateus Getulio.

Author
Posts
#2670091

Hello Toolset team,

I want to have the same functionality as it is described in this thread here: https://toolset.com/forums/topic/prefix-child-with-parent-data/

In my case I want that the address of the parent CPT aktivitaten will be copied into the address field of the child CPT bewertung. To make it quickly understandable: This is a rating system where users can rate activities and the aim is that the rating posts showing the address of the activity post by simply copying it.

So I took the code from the mentioned thread changed the fields which I thought are relevant and added this to my function.php. Unfortunately this does not work.

Can you please have a look on it and let me know where the error is?

Thank you and best wishes
Andreas

#2670093

This is the code I used:

/**
* Copy parent address to child address
*/
add_action('cred_save_data', 'save_parent_fields_to_child_fields',10,2);
function save_parent_fields_to_child_fields($post_id, $form_data) {

// update this to match your CRED form ID
if ($form_data['id'] == 24)
{
$parent_id = $_POST['_wpcf_belongs_aktivitaten_id'];

$parent_first_name = get_post_meta($parent_id, 'wpcf-adresse', true);

// update the child custom fields
update_post_meta($post_id, 'wpcf-adresse_bewertungen', $parent_first_name );
}
}

#2670203

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hey there,

Since Types version 3.x the usage of _wpcf_belongs_{parent-post-type-slug}_id has been discontinued. Now you need to use toolset_get_related_post when there can be only one result, e.g. you are starting with the ID of a child post and you want to get its parent (there can be only one parent, for a given relationship).

Or, you would use toolset_get_related_posts when there could be multiple results, e.g. you are starting with the ID of a parent post and you want to get all of the IDs of all of the child posts for some relationship.

See
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

and

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

I've tried to come up with a code based on your context. Please replace the slug of the relationship in the code bellow and see if it works for you:

<?php
/**
 * Copy parent address to child address
 */
add_action('cred_save_data', 'save_parent_fields_to_child_fields', 10, 2);
function save_parent_fields_to_child_fields($post_id, $form_data) {

    // update this to match your CRED form ID
    if ($form_data['id'] == 24) {

        // no longer valid
        // $parent_id = $_POST['_wpcf_belongs_aktivitaten_id'];

        // replace the slug
        $parent_id = toolset_get_related_post($post_id, 'slug-of-the-relationship', 'parent');

        $parent_address = get_post_meta($parent_id, 'wpcf-adresse', true);

        // update the child custom fields
        update_post_meta($post_id, 'wpcf-adresse_bewertungen', $parent_address);
    }
}

Thank you, please let us know.

#2670391

Hi Mateus,

thank you very much for your quick answer. I adjusted the code but it does not work. My relevant key data for this topic is:

Parent CPT: aktivitaten
Relevant field slug of field-type ‘address’: adresse

Child CPT: bewertung
Relevant field slug of field-type ‘address’: adresse_bewertungen

And the code I added is:

/**
* Copy parent address to child address
*/
add_action('cred_save_data', 'save_parent_fields_to_child_fields', 10, 2);
function save_parent_fields_to_child_fields($post_id, $form_data) {

// update this to match your CRED form ID
if ($form_data['id'] == 24) {

// replace the slug
$parent_id = toolset_get_related_post($post_id, 'aktivitaten', 'parent');

$parent_address = get_post_meta($parent_id, 'wpcf-adresse', true);

// update the child custom fields
update_post_meta($post_id, 'wpcf-adresse_bewertungen', $parent_address);
}}

Can you see any issue?

Best wishes and good weekend
Andreas

#2670701

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello,

I would like to request temporary access (wp-admin and FTP) to your site to take better look at the issue. You will find the needed fields for this below the comment area when you log in to leave your next reply. The information you will enter is private which means only you and I can see and have access to it.

Our Debugging Procedures

I will be checking various settings in the backend to see if the issue can be resolved. Although I won't be making changes that affect the live site, it is still good practice to backup the site before providing us access. In the event that we do need to debug the site further, I will duplicate the site and work in a separate, local development environment to avoid affecting the live site.

Privacy and Security Policy

We have strict policies regarding privacy and access to your information. Please see:
https://toolset.com/purchase/support-policy/privacy-and-security-when-providing-debug-information-for-support/

**IMPORTANT**

- Please make a backup of site files and database before providing us access.
- If you do not see the wp-admin/FTP fields this means your post & website login details will be made PUBLIC. DO NOT post your website details unless you see the required wp-admin/FTP fields. If you do not, please ask me to enable the private box. The private box looks like this: hidden link

Please, let me know if you need any additional details. Have a nice day.

#2670907

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hey there,

I was able to fix it by changing a couple of things.

The slug of the relation in the code from 'bewertung' to 'aktivitat-bewertung'.

The hook we're adding this code from cred_save_data to cred_submit_complete.

The second change was necessary because when cred_save_data is called, the parent info has not been saved yet. This means that by that time if we try to retrieve the parent of that post we'll get 0 as result.

I tested it after changing both things and it seems to have the correct behavior now, please test it again.

Best,

#2670929

Great Mateus,

yes it works now. Thank you very much and kind regards
Andreas

andreasK-17 confirmed that the issue was resolved on 2023-12-05 04:45:10.
This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.