Support

Account

Home Forums General Issues Replicate value of one field into another

Solving

Replicate value of one field into another

  • Hi ACF team,

    This question might be one of the dumbest here but.. So I have a CPT which uses custom fields. One of them is a repeater type. It works. Among subfields, I have an input field called party_time. In many cases party_time values has format day.month.year, but unfortunately it includes also ranges like 4-5.05.2020. What I’m trying to achieve is to create another, dynamic field which will show (both on backend/frontend) values from party_time yet limited to last 7 characters. Thanks to that I will always have sth like: 06.2020 / 02.2020 etc. So basically = month.year format.

    How to achieve that? I’ve went through all official docs and have no idea where to even start. Why I want to do it that way and NOT using only substr on frontend? Because I want to have an ability to filter wp_query by this subfield (month.year).

    Thank you for any tips..

  • here is my code but doesnt work:

    
       function my_acf_load_field( $field ) {
            $current_date = get_sub_field('data');
            $trimmed = substr($current_date, -7);
            $field['default_value'] = $trimmed;
            $field['disabled'] = 1;
            return $field;  
        }
        add_filter('acf/load_field/name=month_year', 'my_acf_load_field');
  • It might be better to use the update_sub_field function inside the save_post action.

    So you would loop through the subfields grab the value, run your substr function, and update the other field with the new value.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.