Support

Account

Home Forums General Issues ACF field that returns value, based on date formula (years since ACF date field) Reply To: ACF field that returns value, based on date formula (years since ACF date field)

  • acf/format_value

    
    add_filter('acf/format_value/name=years_employed', 'calculate_years_employed', 20, 3);
    function calculate_years_employed($value, $post_id, $field) {
      // don't really care what the value is
      $start_date = get_field('start_date', $post_id);
      $value = calculate how long from start date...
      return $value;
    }
    

    this has not been tested at all, but I am pretty sure that the field I’m calling “years_employed” and be a text field with no value at all that is never stored. Because we are ignoring whatever value this field might have and generating a new formatted value.

    This could also be accomplished with an acf/load_value filter.

    Just one note. In order to use either of these filters with the field name the field must actually exist in the DB event if the field has no value.

    If you wanted, for completeness, you could create and acf/save_post filter that would automatically populate the value, but like I said, I think it would work fine using a text field where an empty value is stored in the DB.