Skip Navigation

[Resolved] Populate post custom fields with parent custom field value

This support ticket is created 2 years, 8 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/Hong_Kong (GMT+08:00)

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 2 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#2351087

Hello,

I'm trying to update a post field with a field value of the parent post.

Following exactly this post: https://toolset.com/forums/topic/populate-relationship-m2m-custom-fields-with-parent-child-titles/

I need to do like this BUT instead of post_title use a custom field value:

update_post_meta( $post->ID, $field_for_parent_slug, $get_results[0]['parent']->post_title );

I tried like this but didn't work:

update_post_meta( $post->ID, $field_parent_field_for_parent_slug, $get_results[0]['parent']->'wpcf-parent-field-slug' );
#2351331

Hello,

Please try to modify the codes you mentioned above from:

...
update_post_meta( $post->ID, $field_parent_field_for_parent_slug, $get_results[0]['parent']->'wpcf-parent-field-slug' );
...

To:

...
$parent_post_id = $get_results[0]['parent']->ID;
$parent_field_value = get_post_meta($parent_post_id, 'wpcf-parent-field-slug', true);
update_post_meta( $post->ID, $field_parent_field_for_parent_slug, $parent_field_value);
...

More help:
https://developer.wordpress.org/reference/functions/get_post_meta/