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
#1196963

Hi,

I have kind of relationship, Folders and Files. Now i would like to move Files from one folder to another like following:

Folder1 has File1 and Folder2 has File2. When i move File2 to Folder1 this works great. i achieve this with an edit form of the Files and then select where to Move the Files with the relationship field

[cred_field field="@folder-file.parent" class="form-control" output="bootstrap" select_text="--- not set ---"]

now i want when submitting this relationship form to get the SLUG of the newly selected folder (parent) and add it to a custom file(child) field

summary:

1- select new parent
2- submit the form
3- child saves field value from parent slug
4- parent of child changed

is that possible and what is the best way to do it?

#1197005

One mor question about post relationship.

Post relationships are working only when the posts are public. If i set posts to be Private and try to connect posts withen relationships it doesn't show up any post. i need to be able to select post relationship from the frontend even if my posts are private. where can i changed this?

#1197208

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - sorry, your requirement is still not clear to me what exactly you are looking for.

When you say this:

now i want when submitting this relationship form to get the SLUG of the newly selected folder (parent) and add it to a custom file(child) field

summary:

1- select new parent
2- submit the form
3- child saves field value from parent slug
4- parent of child changed

Do you mean you want to change the child post title or something? Can I have more details with few screenshot or test case example that will help me to understand your issue and guide you in the right direction?

#1197220

I have custom field called secret-id-1. Each time i create a child post this secret-id-1 has as default value the current page slug where this child is getting created.

When changing parent of child the slug of the new parent need to replace the old value of this field with the new slug name if parent.

This means when i change parent of a child two things changes for child:
1-the parent changes
2-custom field value of secret-id-1 get modified and has new value the same value as parent slug..

#1197221

Minesh
Supporter

Languages: English (English )

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

So, when you say parent slug, do you want to assign the parent post slug to secret-id-1 field or it should be assigned the page slug from where you are assigning the parent?

#1197467

Ecactly this "So, when you say parent slug, do you want to assign the parent post slug to secret-id-1 field"

This form should on Submission assign the parent slug to the child post secret-id-1 field. Yes

#1197828

Minesh
Supporter

Languages: English (English )

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

So, what if you try to use the Toolset form hook cred_save_data and based on parent ID you get try to get the parent post slug and assign that value to your desired field secret-id-1.

If you do not know how to do it, I need problem URL and access details so I can check your setup and try to help you further and lead you to the solution.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1198376

I have following code provided by you from another ticket. how can we modify this code so we get the slug of the parent and insert it into custom field of child, which is "wpcf-secret-id-1" ?

add_action('cred_save_data', 'post_title_from_fields', 10, 2);
function post_title_from_fields($post_id, $form_data) {
  
	 if ( ($form_data['id'] == 8603) OR ($form_data['id'] == 8599) ){
 
$field1 = get_post_meta($post_id, 'wpcf-general-first-name', true);
$field2 = get_post_meta($post_id, 'wpcf-general-last-name', true);   
  
    $post_title = $field1 . ' ' . $field2; 
$slug = sanitize_title($post_title); 
  
wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title,'post_name' => $slug));
}
}
#1198378

Minesh
Supporter

Languages: English (English )

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

Well - you can use the following three line of code to get parent post slug and assign the parent post slug to the field wpcf-secret-id-1.

For example:

// get the parent post ID
$parent_post_id = toolset_get_related_post($post_id, 'folder-file' );

//fetch the parent post object
 $parent_obj =  get_post($parent_post_id);

// assign the parent post slug to field wpcf-secret-id-1
update_post_meta($post_id,'wpcf-secret-id-1',$parent_obj->post_name);
#1198387

I have tried following code but this didnt make it.

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' );

    //fetch the parent post object
     $parent_obj =  get_post($parent_post_id);

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

    }
}
#1198401

Minesh
Supporter

Languages: English (English )

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

Well - can you give me problem URL and access details so I can check whats going wrong on your site.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1198571

Minesh
Supporter

Languages: English (English )

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

Do you mean that you want to copy the "Secret ID 2" value of the "Folder 1" to the child post (file)?
=> hidden link

#1198589

Yes, This could be in this case possible since the value of 'Secret ID 2' of Folder 1 is the same value as the Slug of Folder 1.

so this means
Copy Secret ID 2 of Folder 1 to Secret ID 1 of File

#1198591

i tried to copy the Secret ID 2 of one of one of the folder to Secret ID 1 of the File, i think i got a problem the file not showing anymore in the right folder because the letters are capitalized

#1198608

Minesh
Supporter

Languages: English (English )

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

Well - I changed the code to this and it looks like its working:

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' );

    //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);

    }
}

As you can see with the file - Parent Secret ID 2 == file secret ID 1:
=> hidden link

I do not know from where the secret ID is changed as Tooslet does not do such things automatically. Do you need to store secret ID 1 of file field to small letters?