We rely heavily on Advanced Custom Fields (ACF) for custom WordPress development. It is one of those plugins that simply saves lots of time by providing amazing functionality with an easy-to-use interface. During our latest project we ran into some issues trying to query the system to find custom post type (CPT) entries that had an attribute between two dates. This post will explain the issue and how we solved it.
The idea is very simple: there is a page with two DatePickers, one for the start date and another one for the end date of the query:
The user selects these two values and all posts that have the ‘volunteer_training_date’ between those two values are returned. For context, ‘volunteer_training_date’ is a Date Picker field within a Repeater called ‘volunteer_probity_check_and_training_stage’.
When the form was submitted, we fetched the values and passed them as DateTime objects to the meta query. However, this yielded no results at all. The call was now in ACF’s hands and debugging it would be very difficult.
We headed over to the documentation page and found the following info:
The date format that is returned when loading the value. Please note that the value is always saved as YYYYMMDD in the DB.
We changed the DateTime objects to be constructed using that date format and that solved the issue. The code snippet follows:
https://gist.github.com/csaborio001/c66931cc57adce446bfc8dcfa21aad4c
It is so comforting when the last idea you have on your head simply works 🙂