Home Forums ACF PRO How to auto-populate a field getting values from other fields?


How to auto-populate a field getting values from other fields?

  • I want to generate a key that should be auto-populated from other fields values. For example, I have two fields called country and year. These two fields values need to be added to another field called key.

    I have tried so far, not working though.

    function my_acf_load_field( $field ) {
    $countrycode = get_field(‘country’);
    $year = get_field(‘year’);
    $field[‘default_value’] = $countrycode.$year;
    return $field;
    add_filter(‘acf/load_field/name=key’, ‘my_acf_load_field’);

  • You’re trying to set the default value. The default value is only used on new posts. When this is first run on a new post, both of the other fields will have no value so you’re setting the default value to nothing. After the post is saved the first time the default value is ignored.

    If you want this field set based on the other fields then I would suggest an acf/save_post action that updates this field when the post is saved,,

  • Here is the thing. I wanted to generate a number for this format “YEAR.MONTH.post_id” I can do that in PHP by concatenating the_field() values. But that generated number is not showing in the advanced search results. That is why I was wondering if that number could be automatically populated in ACF getting values from other fields.

  • I don’t know what you mean by advanced search results but by using an acf/save_post action you can get the values of other fields, or values from other things in the post and update a field to have whatever value you want it to have.

  • Thanks. did it. I have used update_field( $field_key, $value, $post_id ); and it worked.

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

The topic ‘How to auto-populate a field getting values from other fields?’ is closed to new replies.