So, in the child theme functions.php I have added this code at the end:
function event_date_year_on_save( $post_id ) {
$post_type = 'dig_veranstaltungen'; //custom post type for events
//Check if we are saving correct post type
if( get_post_type( $post_id ) != $post_type)
return;
//Check it's not an auto save routine
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
// get the event date of the post. Note this is in YYYYMMDD format
$event_date = get_post_field ('veranstaltungs_datum_beginn', $post_id);
// update the event year value
update_field( 'jahr', substr( $event_date , 0, 4 ), $post_id );
}
add_action('save_post', 'event_date_year_on_save');
It is really easy to get the year because it is the first four characters of the event date!
Try it for yourself. Edit and event and change the event date (to a different year). No need to change the Year (jahr) you will see it changes automatically.
I also added a little bit of CSS (in the theme customizer) to make the form nicer, and the year field also, plus German labels.
Once you are happy that works, make these changes:
https://www.screencast.com/t/yYKJTzoMp3
Now the user will not see this field in the admin editor.
All you have to do is edit the events and save. You do not actually need to make any changes, the save is to trigger the code to add the year.