Skip Navigation

[Resolved] Get parent CPT data for child CPT form

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

Problem:
How can I update some values of Custom Fields of a Child Post when submitting a CRED Form, with values of Fields stored in the Parent Post of the currently edited Post?

Solution:
You can use the CRED API and WordPress API for this.
It requires Custom Code.
An example is here:
https://toolset.com/forums/topic/get-parent-cpt-data-for-child-cpt-form/#post-427832

Relevant Documentation:
https://developer.wordpress.org/reference/functions/get_post/
https://developer.wordpress.org/reference/functions/update_post_meta/
https://developer.wordpress.org/reference/functions/get_post_meta/
https://toolset.com/documentation/user-guides/cred-api/#csd

100% of people find this useful.

This support ticket is created 8 years, 1 month 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 7 replies, has 2 voices.

Last updated by virageguitars 8 years, 1 month ago.

Assisted by: Beda.

Author
Posts
#427200

I have a parent CPT "type 1" and a child CPT "type 2" (relations one to many).
I create in parent post the child form for "new type 2". Some of the fields of this form must get data from parent post before save (user have to see the data before push the save data).

Can you help me?
Can you give me a sample of code?

Thank you for your time.
Henadz

#427298

Do you want to populate a CRED Form that creates Child Posts, with data from the parent Post?

You can use a limited set of ShortCodes, and they can not be used to populate actual CRED Fields:
https://toolset.com/documentation/user-guides/cred-forms-for-child-content/ > Information about Parent Pages

This is what is disponible and provided by CRED plugin.

Since the Post does not exist at this moment before you submit it, it is not possible to get it's parent Data by the usual PHP, becuase there is actually no entry yet in the Database for this Post.

One possible approach could be this:

1. Create the Child CRED Form link and set it to "Set the parent according to the currently displayed content"

2. When you now click on that Link, it should redirect to the Page with the CRED Form for childs

3. It should append this to the Page Slug/URL:
/?parent_your-post-type_id=123

4. What you now can try to do in your CRED form is to use the Views ShortCode [wpv-search-term] to get that ID.

This means, in your CRED form you use something like this to get the ID of the Parent Post:
ID: [wpv-search-term param='parent_your-post-type_id']

This should also be possible to be used to display Parent Post Infos, as in this example:
[wpv-post-title id='[wpv-search-term param='parent_paar_id']']

With this you can call any info of the parent.

Please aknowledge that this can NOT be used to POPULATE CRED fields.

This is simply not possible because WordPress does not allow nested ShortCodes and Views is the only Plugin that allows this, and we do not extend this support for CRED ShortCodes as well.

But you should be able to achieve your Goal with this, as you can now display ANY parent Data on your CRED form

#427406

Thank you, Beda,

I understood.

Some clarification of real scenario:
- when a user pushes the button "Send a request" (in parent CPT "event")
- after it I have to get one of these variants:
a) email message "request for participation in event" (with data of parent CPT "event" + user data: name etc) + and a user must get email with the same data
or
b) create a child CPT "event request" with some data of parent CPT "event"

What can we do?

Thanks for your time

#427414

A. For this you use CRED Notifications and in there, Body Codes an ShortCodes.

This is all visible in the GUI where you set up notifications in CRED (the form for the Child Posts)

No Front end code or such is needed for this.
https://toolset.com/documentation/user-guides/automated-email-notifications-with-cred/

B. I elaborated on this previously.

You can use the mentioned ShortCodes or use Custom PHP to get Data From the Parent and populate the new Child Post with this.

You can in this case use the cred_save_data Action:
https://toolset.com/documentation/user-guides/cred-api/#csd

In that action you will get_post_meta() the ID of Parent of the Current Saved Post.
This ID is stored in the new Child post in a Custom Field with key "_wpcf_belongs_parent-type-slug_id"

Once you have this ID, you can get_post() the Posts Object of the parent and retrieve any Post Meta from it, with which you can then update_post_meta() your Child Post Meta Data.

This is elaborated in the WordPress API:
https://developer.wordpress.org/reference/functions/get_post/
https://developer.wordpress.org/reference/functions/update_post_meta/
https://developer.wordpress.org/reference/functions/get_post_meta/

#427471

One code sample will be more than many words 🙂
Thanks

#427832

Let's say, I want to get the Field "my field" that is created with Types Plugin and assigend to the parent Post Type (parent-post-type), and once I have it, I want to update the new Child post Field "my other field" with this value.

Then I use cred_save_data() action and WordPress API.

Remember, all Types Custom Fields have a meta key as in "wpcf-your-field-slug"

This is the DOC you will need to study in regard:
https://toolset.com/documentation/user-guides/cred-api/#csd
https://developer.wordpress.org/reference/functions/get_post_meta/
https://codex.wordpress.org/Function_Reference/update_post_meta

This is a Code Sample that will do that:

function my_save_data_action($post_id, $form_data)
{
    // CHANGE THE ID TO YOUR CRED FORM ID (CHILD FORM)
    if ($form_data['id']==12)
    {
       //We get the parent Post ID
       //REPLACE parent-post-type with the slug of your parent post type
        $parent_id = get_post_meta($post_id, '_wpcf_belongs_parent-post-type_id', true);

        //We get the "my-field" value of the parent post
        $my_field = get_post_meta($parent_id, 'wpcf-my-field', true);

        //We update the "my-other-field" with this above value
        update_post_meta($post_id, 'wpcf-my-other-field', $my_field);
    }
}
add_action('cred_save_data', 'my_save_data_action',10,2);
#430655

Thank, Beda!
It's work 🙂 Super!

I need make such code for each fields of one form or I can combine a few fields in one code for one form?
If yes, please, give me the sample.

Thank you for your time
Henadz

#430975

Resolved

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