Support

Account

Home Forums General Issues get year value from date field to set the category

Unread

get year value from date field to set the category

  • hi
    I have a date filed ( the_date ) in a repeater field , also have a custom taxonomy field ( select with multi choice ) with id the_year .

    I need to grap the years only from the date field and set them as a choosen values in the category field of the post .

    as an example when I set 2 values in the date field of ( 22.10.2020 and 21.10.2022 ) the term field must check the 2020 and 2022 categories after saving or updating the post.

    whould you please help with the function to use.

    I tried this function but not working.

    function UpdateYearField($post_id){
    $year_field = get_field('the_year',$post_id);
    $full_date = get_field('the_date',$post_id);
    $year_from_date = DateTime::createFromFormat('Ymd', $full_date)->format('Y'); // will get the year from the $full_date
    
    update_field('the_year', $year_from_date ,$post_id);
    }
    add_action('acf/save_post', 'UpdateYearField', 20);
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.