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 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)
This topic contains 2 replies, has 2 voices.
Last updated by 4 years, 12 months ago.
Assisted by: Christian Cox.