Skip Navigation

[Resolved] Update types custom field in parent post from custom field in child post

This thread is resolved. Here is a description of the problem and solution.

Problem:

I have:
parent post type: "contatto"
child post type "corso"
custom field "data-inizio corso" in child "corso"
custom field "data-ultimo-corso" in parent "contatto"

I would like that when filled "data-inizio-corso" the value is copied in "data-ultimo-corso" updating or creating the post.

Solution:

Types plugin is using a custom field "_wpcf_belongs_[parent-slug]_id" to store the parent post ID, for example, if the parent post type is using slug "contatto", then in single child "corso" post, there is a hidden field "_wpcf_belongs_contatto_id", the filed value is parent post ID, so if "$post_ID" is a child "corso" post ID, you can get the parent "contatto" post ID with PHP codes:

$contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);

Then use the $contatto_id to update the custom field "data-ultimo-corso" to what you want.

Relevant Documentation:

This support ticket is created 6 years, 6 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 3 replies, has 2 voices.

Last updated by Luo Yang 6 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#606587

Hello i have:
parent post type: "contatto"
child post type "corso"
custom field "data-inizio corso" in child "corso"
custom field "data-ultimo-corso" in parent "contatto"

I would like that when filled "data-inizio-corso" the value is copied in "data-ultimo-corso" updating or creating the post.

I followed this thread succefully:
https://toolset.com/forums/topic/calculate-value-of-a-custom-field-based-on-other-custom-fields-values/

and tried with this guide without success:
https://toolset.com/documentation/user-guides/many-to-many-post-relationship/

My code:

function inserisce_data_ultimo_corso( $post_ID ) {      
    if ( get_post_type( $post_ID ) == 'corso' ) {
		        $data_corso_inserito       = get_post_meta($post_ID, 'wpcf-data-inizio-corso', true);
		        update_post_meta( $post_ID, 'wpcf-data-ultimo-corso-test', $data_corso_inserito );
	}
}
		add_action( 'save_post', 'inserisce_data_ultimo_corso', 99 );

It allow me to update custom field "data-ultimo-corso-test" if it's in the same post, i didn't understand how to update the parent post.

I know that line
[php]
update_post_meta( $post_ID, 'wpcf-data-ultimo-corso-test', $data_corso_inserito );
[php]
has to be changed and i think i have to to use "wpcf_belongs", but i can't understand how.

Thanks, Luigi

#606663

Dear CulturaI,

Types plugin is using a custom field "_wpcf_belongs_[parent-slug]_id" to store the parent post ID, for example, if the parent post type is using slug "contatto", then in single child "corso" post, there is a hidden field "_wpcf_belongs_contatto_id", the filed value is parent post ID, so if "$post_ID" is a child "corso" post ID, you can get the parent "contatto" post ID with PHP codes:

$contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);

Then use the $contatto_id to update the custom field "data-ultimo-corso" to what you want.

#606797

Thanks Luo it's working good! Works in CRED too.

My code for reference

function inserisce_data_inizio_ultimo_corso( $post_ID ) {      
    if ( get_post_type( $post_ID ) == 'corso' ) {
		        $data_inizio_corso_inserito       = get_post_meta($post_ID, 'wpcf-data-inizio-corso', true);
		        $contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);
		        update_post_meta( $contatto_id, 'wpcf-data-inizio-ultimo-corso', $data_inizio_corso_inserito );
	}
}
		add_action( 'save_post', 'inserisce_data_inizio_ultimo_corso', 99 );
#606959

You are welcome, and thanks for share the codes.

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.