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.
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.
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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re hard at work on ACF 6.1, and Beta 1 is now available 🚀— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.