Skip Navigation

[Resolved] Automatically assign custom field value of the parent to its child in CRED form

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

Problem: I have a Form that is used to create posts. These posts are child posts in a one-to-many relationship with another post type, the parent post. When I create child posts in Forms, I would like to automatically set the value of one field in the child post using the value of another field in the parent post.

Solution: Let's say you have a custom field in the parent post called "start-date", and you want to add that date field information to the child post in a custom field called "child-start-date". To access the parent post's custom field value, you can use the Types field shortcode with the $current_page operator like this:

[types field="start-date" output="raw" item="$current_page"][/types]

This will retrieve the start date field value from the parent post, since the Form you said is placed in the parent post template.

To set the child post's custom field value using the parent field value, you'll use the same shortcode as above and place it in the child post Form's custom field shortcode as the value of the "value" attribute. For example:

[cred_field field='child-start-date' force_type='field' class='form-control' output='bootstrap' value='[types field="start-date" output="raw" item="$current_page"][/types]']

This will set the raw value of the child date field to be equal to the raw value of the parent date field. Since you don't want this value to be editable, you can either hide the child date field with CSS or you can add readonly='true' to a cred_field shortcode to display the value, but lock it.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://toolset.com/documentation/user-guides/front-end-forms/cred-shortcodes/#cred_field

This support ticket is created 5 years 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
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
#1472455

Tell us what you are trying to do?

I am trying to figure out a way to assign the value of some custom fields from the parent post to its child, in a CRED form that creates a child post. The form is located on the same page as the parent post.

Why I need this: when creating the parent post (Company profile), I have set up custom code that takes specific fields of the formand assigns their values as taxonomy terms (4 taxonomies). Now, when this post is created, I have inserted a CRED form in it to allow the user to create a child post (Personal profile). I would like this child to have the same taxonomy terms as the parent, but not to let the user select them again and follow the same procedure as for the parent creation. Instead, I would like them to be automatically assigned as custom fields of the child, so that I can apply the same mechanism as for the parent in order to pass them as taxonomy terms.

Is this doable with Toolset?

Is there any documentation that you are following?
https://toolset.com/forums/topic/display-a-field-in-child-post-cred-form-only-if-parent-post-has-specific-value/
https://toolset.com/forums/topic/how-to-display-custom-field-value-from-parent/

#1473541

Hi, I'm not quite sure I understand how you're using custom fields and taxonomy terms together to transfer information, but I can certainly show you how to apply the parent post's information in the new child post Form. Let's say you have a custom field in the parent post called "start-date", and you want to add that date field information to the child post in a custom field called "child-start-date". To access the parent post's custom field value, you can use the Types field shortcode with the $current_page operator like this:

[types field="start-date" output="raw" item="$current_page"][/types]

This will retrieve the start date field value from the parent post, since the Form you said is placed in the parent post template. Types field shortcode documentation is available here: https://toolset.com/documentation/customizing-sites-using-php/functions/

To set the child post's custom field value using the parent field value, you'll use the same shortcode as above and place it in the child post Form's custom field shortcode as the value of the "value" attribute. For example:

[cred_field field='child-start-date' force_type='field' class='form-control' output='bootstrap' value='[types field="start-date" output="raw" item="$current_page"][/types]']

This will set the raw value of the child date field to be equal to the raw value of the parent date field. Since you don't want this value to be editable, you can either hide the child date field with CSS or you can add readonly='true' to a cred_field shortcode to display the value, but lock it. We have documentation for cred_field shortcodes available here: https://toolset.com/documentation/user-guides/front-end-forms/cred-shortcodes/#cred_field

Please let me know if I've misunderstood what you want to accomplish, or if you're having trouble implementing this technique on a specific field.

#1483597

My issue is resolved now. Thank you!