Support

Account

Home Forums Backend Issues (wp-admin) Saving Default Values

Solving

Saving Default Values

  • I have created a new field with a default value. I have over 500 posts and it seems the only way I can get it to show on the front end is if I go into the edit screen of each post individually and update it. Is there a way I can get this to show without going through each individual post?

    Thanks

  • https://www.advancedcustomfields.com/resources/acfload_value/

    
    add_filter('acf/load_value/name=your_field_name', 'default_value_for_your_field_name', 10, 3);
    function default_value_for_your_field_name($value, $post_id, $field) {
      if ($value === NULL) {
        // value is NULL because it was never saved
        $value = 'my default value';
      }
      return $value;
    }
    
  • Hello

    @hube2 Why is the default_value not available in the $field that enters the hook function? That would help to set the returned value much easier.

    Thank you

  • This is a very old post. In fact, the filter that I posted 3 years ago will not actually do anything and never would have.

    In order to have ACF return default values when a field has never been saved to the DB you must use the field key. This is the case with any action or filter for any field that has never been saved.

    All ACF functions really operate on the field key. When you use the field name ACF must look up the field key in the DB. If that field has never been save/updated ACF cannot find the field key. Without the field key ACF cannot get field settings for the field.

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

You must be logged in to reply to this topic.