Home › Forums › Backend Issues (wp-admin) › Datepicker save format
Hi,
I use the Rehub theme and have already some custom fields in posts, also a date field with the format “Y-m-d” (the theme use this format). I want to use ACF to fill this fields in the backend, but when I use the date picker the date will always saved in the format “Ymd” in the custom field. I can change the code in the theme to this format, but with an update from the theme it will always be overwritten. So it is possible with ACF to save the date in the format “Y-m-d” in this custom field?
Best regards
There isn’t a way to alter the way that ACF saves the value in the DB.
Using a child theme you could possibly use filters to alter the value loaded by ACF, but I would need more information on exactly how you are attempting to use these fields or how the theme is using these fields.
Here’s an example of the different save formats in the DB (with ACF and with the option from the theme): https://imgur.com/a/epHe0TS
The problem is that there are also some plugins who belongs to the theme, which uses this format. And with theme updates (it’s often) they will also be overwritten, I use already a child theme.
So there’s no possibility (in the scripts of ACF) to change the date-format in Y-m-d?
Like I said, you can’t alter the format ACF uses to save the date value because that would cause ACF to not be able to display the fields for editing.
What you need to do is to figure out a way, if it is possible at all, to filter the values when the theme gets/uses them to match the format that is needed there. This depends on how the theme or another plugin is getting the value.
For example, if the theme or plugin is using get_post_meta() to get the value you could possibly add a filter on the "get_{$meta_type}_metadata"
hook called in the WP function get_metadata_raw() to alter the value being returned.
The theme gets the value in the script by
get_post_meta( $postid, 'rehub_offer_coupon_date', true )
So you mean I can add in the functions.php this filter?
Yes
add_filter('get_post_metadata', 'format_rehub_offer_coupon_date', 10, 5);
function format_rehub_offer_coupon_date($value, $object_id, $meta_key, $single, $meta_type) {
// $value will initially be NULL
// this happens before data is retrieved
// returning anything other than NULL will result in short circuit of WP function
// this might break the acf input in admin
// if it does un-comment the following if block
/*
if (is_admin()) {
return $value;
}
*/
// see if it's the meta key we want to format
if ($meta_key != 'rehub_offer_coupon_date') {
// not our field
return $value;
}
// remove this filter to prevent infinite loop
remove_filter('get_post_metadata', 'format_rehub_offer_coupon_date', 10);
// get the value of field
$new_value = get_post_meta($object_id, 'rehub_offer_coupon_date', true);
// return if empty
if (empty($new_value)) {
return $value;
}
// correctly format the value for theme
$value = date('Y-m-d', strtotime($new_value));
// re-add this filter
add_filter('get_post_metadata', 'format_rehub_offer_coupon_date', 10, 5);
// return new value
return $value;
}
Hey, that looks great!!!
The only thing is, that in the date picker field there is always the date “01.01.1970” , but in the custom field it saves the correct date. https://imgur.com/a/JXIIybQ
The code is effecting the admin, See my not in the code I gave you about disabling the filter in the admin
Ah okay, I was not sure what this mean.
But thank you very very much, now it’s perfect!
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 use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.