Skip Navigation

[Resolved] Display value from parent post field on child post form

This support ticket is created 5 years, 5 months 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by julieP 5 years, 5 months ago.

Assisted by: Waqar.

Author
Posts
#1256395

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

#1256749

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

#1256761

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.

#1256869

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

#1262485

That's great Waqar thank you.