[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.
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?
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.
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.
- 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.
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.