Skip Navigation

[Resolved] Get custom field value from Parent into Child when changing Parent

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

Problem:
Get custom field value from Parent into Child when changing Parent

Solution:
You can use the Toolset form's hook "cred_save_data" to get the parent ID and based on that get parent field value.

You can find the proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/get-custom-field-value-from-parent-into-child-when-changing-parent/page/2/#post-1198672

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Author
Posts
#1198637

Thank you Minesh it looks like the snippet is saving the secret ID 2 of the old Parent and not the new Parent.

I tired following:

1- created Folder 3 and Folder 4
2- Added a File to Folder 3
3- Changed the Parent of the File to Folder 4
4- The file Disappeared from Folder 3 as well not showing in Folder 4

i Checked on the backend this is what happening:

1- when we submit the form 203, the snippet look for the custom field Secret ID 2 of current Parent and add it the custom field Secret ID 1 of child. what we need is to add the Secret ID 2 of the new Parent to the Secret ID 1.

2- Second problem is that the Secret ID 2 is a secret key that gets automatically generated with combination of small and capital letters. so when we just copy the Secret ID 2 to Secret ID 1, this copies exactly the same which is causing the files to not show up in any folder. because in the views i have conditional to show only files and folders with the secret ID 1 has the same value of the current page slug. this means when we are in Folder 2 and create Folder 3 in there the current slug of the Folder 2 get saved in the Secret ID 1 of Folder 3 as small letter because its taking the value from the slug.

I know it sounds very complicated, and even more complicated for me to explain this. but the best what we can do, is to get the slug name of the new parent to save in the child Secret ID 1.

#1198640

I have uncovered the forms for creating folders and files.. you will see the secret keys there. Secret ID 1 is taking from the current page slug and the Secret ID 2 is getting by each page refresh generated.

#1198672

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I've again adjusted the code as given under:

add_action('cred_save_data', 'parent_slug_to_custom_field', 10, 2);
function parent_slug_to_custom_field($post_id, $form_data) {
   
    if ( ($form_data['id'] == 203) OR ($form_data['id'] == 1111) ){
           
    // get the parent post ID
   // $parent_post_id = toolset_get_related_post($post_id, 'folder-file' );
     
    $parent_post_id = $_POST['@folder-file_parent'];
   
    //fetch the parent post object
     $parent_sec_id_2 =  get_post_meta($parent_post_id,'wpcf-secret-id-2',true);

    // assign the parent post slug to field wpcf-secret-id-1
    update_post_meta($post_id,'wpcf-secret-id-1',$parent_sec_id_2);

    }
}

If you will check database - I see that the correct field values are fetched and stored in database. Can you please confirm. Now I think the only issue you have is to display it and I do not know how and where you are displaying it.

#1198688

Yes, You got it!

I think the only issue now is to make the text saved in Secret ID 1 to be small letters? once they are small letters the issue is resolved.

#1198695

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Great - thanks for confirmation.

Again, I chagned the following line:

// assign the parent post slug to field wpcf-secret-id-1
    update_post_meta($post_id,'wpcf-secret-id-1',$parent_sec_id_2);
</cide>
To (I've applied strtolower() function)

// assign the parent post slug to field wpcf-secret-id-1
update_post_meta($post_id,'wpcf-secret-id-1',strtolower($parent_sec_id_2));
[/php]

And I can see the file in Folder 4 as well - Can you please confirm, Really glad to help you out with this really hard issue.

#1198704

I am so happy with your support seriously. You did a hard job to get this issue solved, and i am glad you were so patient to understand this although its complicated Issue yes! 5 Stars for you Minesh. Thank you very much 🙂

#1198705

My issue is resolved now. Thank you!