Home Forums ACF PRO Storing date fields?


Storing date fields?

  • Hi all,

    I am creating a post via an ajax request which send a date format DD/MM/YYY to the post via update_field.

    In the acf admin I have set display format to the below.

    Date field options

    I have a custom column on the post screen which shows the release date as set but when I open the post for the first time, the date field is then blank and I have to add it all over again.

    Is there some problem with the date format I am selecting, as to way it removed it when the post loads for the first time after creation?

  • How are you inserting the date into the database? Are you using update_field() or another method? What is the date format of the date that you’re inserting into the database?

  • I have mentioned above but:

    Date format is DD/MM/YYYY
    Field is: update_field()

  • First problem is that the format you’re attempting to store is incorrect. ACF may display and return values in the format you set but it always stores date fields in the format “YYYYMMDD”

    The second problem could be related to the adding a new post where the custom field does not exist yet or updating and existing post that already has a date value. When using update_field() for new post you always need to use the field key and not the field name.

  • Hi John,

    The post is actually created first and then the fields are updated after the post creation is complete.

    The storing of the field value correctly seems to be the problem. Swapping to YYYYMMDD works but for some reason I still need to open the post and re-save for it to the start returning it as DD/MM/YYYY. It is like ACF if doing something else once it laods and resaves the value from YYYYMMDD.

    Is there a way of saving it YYYMMDD but without having to then go into the post and resaving for it to return it in the other value?

  • The reason that you need to open the post and save it is because that field_key is not added correctly to the database. When a new post is created the custom ACF field does not exist and this means that acf does not know exactly what to do with the field. This is why you need to use the field key when updating the field so that ACF knows that this is a date field and also inserts the correct field key reference into the database.

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

The topic ‘Storing date fields?’ is closed to new replies.