Issues with ACF and Dates

Warning
This blog post is included for archival purposes and may contain outdated information. While it provides historical insights, we strongly recommend that you double-check the details before relying on any of the information outlined here.

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 🙂

About Author

Christian Saborio

Christian is a seasoned computer engineer with a rich career spanning collaborations with industry leaders such as Artinsoft (now Mobilize.net), Microsoft, HP, and Intel. As a technical evangelist and trainer, Christian honed his expertise in Costa Rica and Seattle, delivering impactful solutions and sharing his knowledge.

Now based in Sydney, Australia, Christian channels his passion into web development, leading a talented team to tackle diverse projects with innovation and precision. His commitment to crafting exceptional digital experiences reflects his deep-rooted enthusiasm for technology and problem-solving.

Comments

    Comments are closed

    Thank you for your interest. Please fill out this form to provide us with your contact information. We'll get back to you as soon as possible.