i recently have problems with a query when trying to compare dates saved with ACF. The dates for some of my posts get saved diffrently than other posts – using the same date fields. The output with get_field() displays just right – in get_post_custom() its shows up differently.
See Post 34687 for example:
The fields get populated with update_post_meta($id, ‘verleih_start’, date(“Y-m-d”, strtotime($verleih_start))); but even when changing the date in backend the format stays the same.
The meta_query i use (work with posts which have the timestamps with hyphens):
'meta_query' => array( array( 'relation' => 'AND', array( 'key' => 'wpcf-transport_status', 'value' => '0', 'compare' => '!=', ) , array( 'key' => 'verleih_start', 'value' => date('Y-m-d', strtotime('+90 days')), 'compare' => '<=', 'type' => 'DATE' ), array( 'key' => 'verleih_start', 'value' => date('Y-m-d',strtotime("today")), 'compare' => '>=', 'type' => 'DATE' ) ), ),
ACF has altered the way is stores and returns values. This was done when the new date/time and time fields were added in order to make dates compatible across these field types.
YYYYMMDD is the way that ACF used to store dates. YYYY-MM-DD is the way that is stores dates now. When loading the value for a field ACF checks to see if the format is the second one and if not it adds the dashes for compatibility, but it does not update the existing value unless you edit the old post where the date is stored and update it.
What you’ll need to do is check the formatting and add the dashes yourself if you are getting values without using ACF.
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!