Skip Navigation

[Resolved] Copy parent and grandparent fields to child post

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

Problem: I would like to use a CRED form to create a child post. When the post is saved, I would like to copy some custom fields from the parent and grandparent posts into the child post.

Solution: Use the CRED API cred_save_data method to automate this process. The link between parent / child posts is saved as a custom field on the child post with the name syntax "_wpcf_belongs_" + parent post type slug + "_id". Here is an example:

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'] == 12345)
  {
// get the parent chosen in CRED form and use that to get parent and grandparent information
$parent_id = $_POST['_wpcf_belongs_werknemers_id'];
$parent_first_name = get_post_meta($parent_id, 'wpcf-voornaam-medewerker', true);
$parent_last_name = get_post_meta($parent_id, 'wpcf-achternaam-medewerker', true);
$grandparent_id = get_post_meta($parent_id, '_wpcf_belongs_werkgever_id', true);
$grandparent_first_name = get_post_meta($grandparent_id, 'wpcf-voornaam-werkgever', true);
$grandparent_last_name = get_post_meta($grandparent_id, 'wpcf-achternaam-werkgever', true);

// set the child post custom field values
update_post_meta($post_id, 'wpcf-voornaam-medewerker', $parent_first_name );
update_post_meta($post_id, 'wpcf-achternaam-medewerker', $parent_last_name );
update_post_meta($post_id, 'wpcf-voornaam-werkgever', $grandparent_first_name );
update_post_meta($post_id, 'wpcf-achternaam-werkgever', $grandparent_last_name );
 }
}

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

This support ticket is created 6 years, 8 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.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#605537
Schermafdruk 2018-01-11 20.29.37.png

Tell us what you are trying to do?

I have a parent cpt 'werknemers' and a child cpt 'beoordelingsformulier'

When I create a new child cpt 'beoordelingsformulier' I want on submission fill fields on the CRED with data from the parent 'medewerkers'.

On the child form is a dropdown where the user can choose the parent.

The fields I want to prefill are the same in the parent and the child.

I sent you the screen images of what I want to achieve.

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#605550

Hi, I will try to help, and I need some more information. In your CRED form I see these fields:
- Voornaam medewerker
- Achternaam medewerker

What do these fields do when the form is submitted? Are these custom fields on the Beoordelingsformulier post, are they used to modify the parent Werknemers post, or only to display the selected parent's information?

#605564

Hi Christan,

Thanks for your quick reply.

What I want is to pull the fieldsdata from the parent cpt tot the child.

For now the fields are custom fields on the beoordelingsformulier. So yes I want to to display the selected parent's information in the fields and on saving the form save these fields also to the child beoordelingsformulier.

#605567

So yes I want to to display the selected parent's information in the fields and on saving the form save these fields also to the child beoordelingsformulier.
Is there a specific reason you want to save parent custom field information in the child post's custom fields? In general, this is not recommended because you save the same data in two places. If the parent ever changes name you must update the child's fields as well, which is unnecessary. You can easily access parent post information from a child post using Types fields or Views shortcodes. More info about displaying parent post information:
https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/
So I urge you to avoid saving Parent post information into the Child post custom fields unless you have a very good technical reason.

When the User selects a Parent, they will see the selected Parent's name in the select field, right? So showing the name again underneath it seems...unnecessary. It also requires quite a lot more work to set this up, because modifying input field values based on another input field isn't a built-in feature of CRED. If it's necessary we can discuss a couple of options, but I urge you not to use these parent name fields in the child CRED form.

#605569

Christian,

I'll explain:

On the data of 'beoordelingsformulier' I need an export tot csv / excel. It's a big cpt with many fields. It's needed tot review students and the data is used tot make reports in Word (Merging the fields tot a Word document with WPtables reporter.

Basically we need te data form 'werkgever' thats the parent of 'medewerkers' and that's the parent of 'beoordelingsformulier'. I prefer a sql query to make the dataset I need. But that's very complicated for me. To query the toolset structure is not regular.

So what I try to do is make 1 cpt with the fields I need to export. Then I use wp all export tot export the data to a cvs file.

Yes I know the solution is far from beautiful but my customer needs the merge function to make custom reports in Word and Excel.

Maybe you have a beter solution for me tot export the data form the three cpt to csv?

#605576

Okay thanks, it seems like you have a technical reason for this structure, so I understand. I think the best way to handle this is to use the cred_save_data API to save these fields automatically. You do not need to include the fields in the CRED form. Here's a sample of how this might work:

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'] == 12345)
  {
    $parent_id = $_POST['_wpcf_belongs_werknemers_id'];
    
    $parent_first_name = get_post_meta($parent_id, 'wpcf-voornaam-medewerker', true);
    $parent_last_name = get_post_meta($parent_id, 'wpcf-achternaam-medewerker', true);
    
    // update the child custom fields
    update_post_meta($post_id, 'wpcf-voornaam-medewerker', $parent_first_name );
    update_post_meta($post_id, 'wpcf-achternaam-medewerker', $parent_last_name );
  }
}

- Change 12345 to match your CRED form ID
- Change '_wpcf_belongs_werknemers_id' to include your post type slug. For example, if your post type slug is "werks" then the code should be "_wpcf_belongs_werks_id".
- In $parent_first_name and $parent_last_name, change 'wpcf-voornaam-medewerker' and 'wpcf-achternaam-medewerker' to match the custom field names on the werknemers post. Add the prefix "wpcf-" to the field slugs.
- In update_post_meta, change 'wpcf-voornaam-medewerker' and 'wpcf-achternaam-medewerker' to match the custom field names on the beoordelingsformulier post. Add the prefix "wpcf-" to the field slugs.

Let me know if you need additional assistance with this type of custom code.

#605578

Christian,

Thank you very much.

So I put this in functions.php ant that's it?

I'll try it i the morning because it's already night over here. I'll let you know how it works.

Jacques

#605581

So I put this in functions.php ant that's it?
Add that code to the functions.php file and change the slugs and numbers like I described. Delete the parent name fields from the CRED form, and try it out! Let me know the results.

#605583

Christian,

I'm still here 🙂

It works for the two fields!! Thank you very very much.

I think I can figure out tot add the other fields I want tot store from the parent in the child.

How does this works with the data form the parent of the parent ('werkgever' - 'werknemer' - 'beoordelingsformulier.

And

Can I add the add-action twice to functions.php. When I copy the action and change the cred number for another form witch is editing the cpt 'beoordelingsformulier' I get a white screen in WordPress.

Jacques

#606069

How does this works with the data form the parent of the parent ('werkgever' - 'werknemer' - 'beoordelingsformulier.
I'm sorry, I don't understand the question. Could you explain in more detail what you would like to accomplish?

When I copy the action and change the cred number for another form witch is editing the cpt 'beoordelingsformulier' I get a white screen in WordPress.
You can't have two functions with the same name. So if you copy and paste the "save_parent_fields_to_child_fields" function just like this, this will cause an error. If you want to reuse the code in another place, change the function name in both places like this:

add_action('cred_save_data', 'save_other_fields_to_other_fields',10,2);
function save_other_fields_to_other_fields($post_id, $form_data) {
...

The function name is arbitrary, but must be different from the original function name.

#606104

Christian,

Thanks so far. The functions are exactly what I need!!

The parent question:

How does this works with the data form the parent of the parent ('werkgever' - 'werknemer' - 'beoordelingsformulier. =

Now when I'm in the cred form for 'beoordelingsformulier' it gets the data from the parent 'medewerker'. CPT 'medewerker' has a parent CPT: 'werkgever'. Can I with a similar function getting fields from the parent 'werkgevers' and put them in 'beoordelingsformulier'.

Jacques

#606117

You can use the same concept to access grandparent post information. Get the grandparent post ID from the parent post, then get its post meta field values. Assign those values to the beoordelingsformulier custom fields. Here's the idea:

$parent_id = $_POST['_wpcf_belongs_werknemers_id'];
$parent_first_name = get_post_meta($parent_id, 'wpcf-voornaam-medewerker', true);
$parent_last_name = get_post_meta($parent_id, 'wpcf-achternaam-medewerker', true);
$grandparent_id = get_post_meta($parent_id, '_wpcf_belongs_werkgever_id', true);
$grandparent_first_name = get_post_meta($grandparent_id, 'wpcf-voornaam-werkgever', true);
$grandparent_last_name = get_post_meta($grandparent_id, 'wpcf-achternaam-werkgever', true);

update_post_meta($post_id, 'wpcf-voornaam-medewerker', $parent_first_name );
update_post_meta($post_id, 'wpcf-achternaam-medewerker', $parent_last_name );
update_post_meta($post_id, 'wpcf-voornaam-werkgever', $grandparent_first_name );
update_post_meta($post_id, 'wpcf-achternaam-werkgever', $grandparent_last_name );
#606121

Christian,

As always, You are the best.

Jacques

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.