Skip Navigation

[Resolved] Copy child date custom field to parent date custom field

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

Problem: I would like to use the save_post hook to update a parent's date field when a child post is saved, but only if the child's date field is a later date.

Solution:
A date field is saved in the database as a timestamp, so you can access the raw data using get_post_meta and compare the child and parent field values. If the child timestamp is larger, insert it into the parent field.

[php]
//DATA FINE ULTIMO CORSO
function inserisce_data_fine_ultimo_corso( $post_ID ) {
if ( get_post_type( $post_ID ) == 'corso' ) {
$data_fine_corso_inserito_timestamp = get_post_meta($post_ID, 'wpcf-data-fine-corso', true);
$contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);
$data_fine_corso_attuale_timestamp = get_post_meta( $contatto_id, 'wpcf-data-fine-ultimo-corso', true );
if ( $data_fine_corso_inserito_timestamp > $data_fine_corso_attuale_timestamp) {update_post_meta( $contatto_id, 'wpcf-data-fine-ultimo-corso', $data_fine_corso_inserito_timestamp );}
}
}
add_action( 'save_post', 'inserisce_data_fine_ultimo_corso', 99 );

This support ticket is created 6 years, 2 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by culturaI 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#610427

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

My objective:
Using CRED and update "data-fine-ultimo-corso" only if is smaller than "data-fine-corso"

I added my basic function:

//DATA FINE ULTIMO CORSO
function inserisce_data_fine_ultimo_corso( $post_ID ) {      
    if ( get_post_type( $post_ID ) == 'corso' ) {
		        $data_fine_corso_inserito       = get_post_meta($post_ID, 'wpcf-data-fine-corso', true);
		        $contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);
		        update_post_meta( $contatto_id, 'wpcf-data-fine-ultimo-corso', $data_fine_corso_inserito );
	}
}
		add_action( 'save_post', 'inserisce_data_fine_ultimo_corso', 99 );

Until now is working good, so i tried to add a condition IF and changed this way:

//DATA FINE ULTIMO CORSO
function inserisce_data_fine_ultimo_corso( $post_ID ) {      
    if ( get_post_type( $post_ID ) == 'corso' ) {
		        $data_fine_corso_inserito       = get_post_meta($post_ID, 'wpcf-data-fine-corso', true);
		        $data_fine_corso_inserito_timestamp = strtotime($data_fine_corso_inserito);
		        $contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);
		        $data_fine_corso_attuale = get_post_meta( $contatto_id, 'wpcf-data-fine-ultimo-corso', true );		        
		        $data_fine_corso_attuale_timestamp = strtotime($data_fine_corso_attuale);
         if ( $data_fine_corso_inserito_timestamp > $data_fine_corso_attuale_timestamp) {update_post_meta( $contatto_id, 'wpcf-data-fine-ultimo-corso', $data_fine_corso_inserito );}
	}
}
		add_action( 'save_post', 'inserisce_data_fine_ultimo_corso', 99 );

This don't seem to work and i can't manage how to do it, can i use some help?
Thanks!

#610508

Hi, Types date fields are saved in the database as timestamps, so there's no need to use strtotime to convert those values. You can remove that conversion code and use the raw values from the database:

//DATA FINE ULTIMO CORSO
function inserisce_data_fine_ultimo_corso( $post_ID ) {      
    if ( get_post_type( $post_ID ) == 'corso' ) {
                $data_fine_corso_inserito_timestamp = get_post_meta($post_ID, 'wpcf-data-fine-corso', true);
                $contatto_id = get_post_meta($post_ID, '_wpcf_belongs_contatto_id', true);
                $data_fine_corso_attuale_timestamp = get_post_meta( $contatto_id, 'wpcf-data-fine-ultimo-corso', true );
                if ( $data_fine_corso_inserito_timestamp > $data_fine_corso_attuale_timestamp) {update_post_meta( $contatto_id, 'wpcf-data-fine-ultimo-corso', $data_fine_corso_inserito_timestamp );}
    }
}
        add_action( 'save_post', 'inserisce_data_fine_ultimo_corso', 99 );

#610510

Thanks Christian working good!

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