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!
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 );
Thanks Christian working good!