Support

Account

Home Forums General Issues Add default Fields to Post programmatically Reply To: Add default Fields to Post programmatically

  • The only way to insert the default values into the database is to do what you describe.

    When I know that posts can be inserted in a way other than by creating a post in the admin then I plan for this ahead of time.

    There are two things that you can do. The first is that when you’re building the templates is to test for values and if there is none then to hard code the default. This can be done is several way depending on your coding style but a simple example is.

    
    if (get_field('some_field_name')) {
      // field has a value, do something
    } else {
      // field does not have a value, do default
    }
    

    Another way is to use the field key when getting a field value.

    
    $value = get_field('field_XXXXXXX');
    

    This will get the default value if there is nothing in the DB. When you use the field name ACF needs to look up the field key. If there is no value in the db for the meta value and field key reference it does not know what field you are looking for. Using the field key bypasses the need to look up the field key and allows ACF to return the default value for the field.