Is there a way of displaying the value of a custom field from a parent post in the form used to create its child? The parent is automatically assigned (the link to the child form is in a View selecting parent posts).
I've tried these but neither are working:-
[types field='date' id='[wpv-post-field name="@teacher-student.parent"]'][/types]
[types field='date' id='@teacher-student.parent'][/types]
Thanks
Hi Julie,
Thank you for contacting us and I'll be happy to assist.
To get the custom fields data from a different post than the current one, the [types] shortcode supports the "item" attribute:
https://toolset.com/documentation/customizing-sites-using-php/functions/
For example, to show the value of the field with slug "date", from the parent post "teacher" in a "teacher-student" relationship, you can use:
[types field='date' item='@teacher-student.parent'][/types]
Here is a guide on the use of "item" attribute:
https://toolset.com/documentation/user-guides/views-shortcodes/item-attribute/
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Hi Waqar
This works when used say in a VIEW which filters for the child posts but I'm trying to achieve this on a FORM that creates a child post.
Hi Julie,
Thanks for writing back and I apologize for missing out that this was needed inside a post form.
In the first message, you mentioned: "the parent is automatically assigned (the link to the child form is in a View selecting parent posts)."
Does this mean that when the page with the form for a child post is viewed, the ID of the parent post is passed on from the view in URL parameter?
e.g. yourwebsite.com/page-with-form/?id=1234
( where 1234 is the ID of the parent )
If this assumption is correct, you can add a custom shortcode, which can get the ID of the parent post from the URL parameter and then get the value of the required custom field from that parent post.
Example:
function show_default_value_parent_field_func( $atts ) {
$a = shortcode_atts( array(
'field' => '',
'param' => '',
), $atts );
ob_start();
echo do_shortcode("[types field='".$a['field']."' item='".$_GET[$a['param']]."'][/types]");
return ob_get_clean();
}
add_shortcode( 'show_default_value_parent_field', 'show_default_value_parent_field_func' );
The above custom shortcode can be added into the active theme's "functions.php" file.
To show the value using this shortcode, you can use it in the form like this:
[show_default_value_parent_field field="field-slug" param="id"]
Note: Please replace "field-slug" with the actual slug of the target field from the parent post and "id" with the actual URL parameter that you're using for passing the parent post.
In case, you're using any different approach for passing the parent post to this form, please share the specific details.
regards,
Waqar
That's great Waqar thank you.