Skip Navigation

[Resolved] get value of parent post 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.

Our next available supporter will start replying to tickets in about 2.06 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Minesh 6 months ago.

Assisted by: Minesh.

Author
Posts
#2691279

Tell us what you are trying to do?

By cred form need to get value of parent post field : wpcf-glavanastavnika
and put it in the field of child post : wpcf-glavamiu
relationship : miuonline_volshebniki

add_action('cred_save_data', 'plus_one_glava_diu_miu_dc',10,2);
function plus_one_glava_diu_miu_dc ($post_id, $form_data)
{
// Execute for form ID: 123
if ($form_data['id'] == 16709 ) {

// Custom field name
$mmeta_key = "wpcf-glavamiu";

// Get value from parent MIU field
global $post;
$parent_post = $post->ID;
$field_slug = 'miuonline_volshebniki';

$get_results = toolset_get_related_posts( $parent_post, $field_slug, 'parent', 1000, 0, array(), 'post_id','child');
$parent_glavamiu = types_render_field( 'wpcf-glavanastavnika', array( 'item' => $get_results) );

// Finally, update $meta_key for $user_id with $newValue

update_post_meta( $post_id, $mmeta_key, $parent_glavamiu);

}
}

Is there any documentation that you are following?
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

Is there a similar example that we can see?

https://toolset.com/forums/topic/how-to-get-value-of-a-field-from-a-custom-post-with-parent-post-reference-field/

What is the link to your site?

#2691351

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand - you have Toolset Form for parent post type and when you submit the form you want to get the specific field value belongs to parent post and assign that field value to child post. If this is true - to What child post you want to assign the field value as child will be many how to know what specific child post is updated with parent custom field value?

Please correct me if I'm wrong and once we clarify the things I will be able to guide you in the right direction.

In addition to that - Could you please send me debug information that will help us to investigate your issue.
=> https://toolset.com/faq/provide-debug-information-faster-support/

#2691427

I have Toolset Form for CHILD post type
and when I submit the form on CHILD post
I want to get the specific field value belongs to PARENT post
and assign that field value to CHILD post.

The form is placed on CHILD post and relationship has only one Parent post.
relationship : miuonline_volshebniki

Sincerely
Anton

#2691431

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You can use the Post relationship API function toolset_get_related_post which is set to return the parent ID value. As you can see with the following Doc:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

Can you please try to use the following modified hook code:

add_action('cred_save_data', 'plus_one_glava_diu_miu_dc',10,2);
function plus_one_glava_diu_miu_dc ($post_id, $form_data)  {
// Execute for form ID: 123
if ($form_data['id'] == 16709 ) {

// child post Custom field name
$mmeta_key = "wpcf-glavamiu";

$relationship_slug = 'miuonline_volshebniki';

// get parent ID
$parent_id = toolset_get_related_post( $post_id, $relationship_slug,'parent');

// get parent custom field value
$parent_glavamiu = get_post_meta($parent_id ,'wpcf-glavanastavnika',true);

// Finally, update $meta_key for $user_id with $newValue
update_post_meta( $post_id, $mmeta_key, $parent_glavamiu);

}
}
#2691543

Thank you a lot!

antonK-2 confirmed that the issue was resolved on 2024-04-09 10:34:42.
This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.