Browser: All
OS: macOS
Hosted: on Local host
Similar posts have been posted regarding this but I can’t find a solution that works with dates in a repeater field, apologies if I’ve overlooked one that does
Dates in ACF are set to return the date as Ymd.
I have a list of courses in a query, within each course there is a ACF Repeater field for a list of course dates available for that course. To query this I use…
Type : ACF Repeater: Course Dates
Without any conditions on the query block I get all course dates listed, This shows {acf_course_dates_start_date:j}, {acf_course_dates_end_date:j} and i’ve included the full date format and current date as Ymd to show what is being compared.
The condition I’ve used should only show dates after the current date…
Dynamic Data
{current_date:Ymd}
<
{acf_course_dates_start_date:Ymd}
This shows no results, but you can see there are dates in both May and June in the screenshot.
If I change the condition to be…
Dynamic Data
{current_date:Ymd}
>
{acf_course_dates_start_date:Ymd}
I get the two dates that are before the current date displaying, March and April.
There seems to be an issue which I can’'t find a solution to.
Regards,
Tony
Hi Tony,
Sorry for the delayed reply.
Please may I know where you place the condition? On the Query loop div?
Kindly place it after executing the query, or you will get the incorrect result.
Reference: SOLVED: Current Date condition not working with ACF Options Page Repeater - #8 by itchycode
Regards,
Jenn
Thank you Jenn for the solution to this.
It does work, but doing it this way will render an empty div when the condition is met and the date is not displayed. Which then causes issue when using grid to display the items.
Can it not be done where if a query loop and condition are added to a same element, the query always runs first?
Regards,
Tony
I guess a solution for the moment is to use css on the div…
%root%:empty {
display:none;
}
To hide the empty div that is rendered.
Hey @trebair,
I am not 100% sure I understood the scenario. But I think you can use ACF’s load_value hook for this:
add_filter( 'acf/load_value/name=course_dates', function( $value, $post_id, $field ) {
if ( is_admin() || ! is_array( $value ) ) {
return $value;
}
$today = wp_date( 'Ymd' );
return array_filter( $value, function( $row ) use ( $today ) {
return $row['field_6633be5909cad'] >= $today;
} );
}, 10, 3 );
In my code course_dates
is the name of the ACF repeater and field_6633be5909cad
is the key of the date field within the ACF repeater. The rest should be self-explanatory.
Example
Course dates in the backend:
Course dates on the frontend:
Let me know if this helps.
Best,
André
2 Likes
Hi André,
Yes this works great on the front end but if I go to edit the template i’m getting a Fatal error…
Fatal error: Uncaught TypeError: array_filter(): Argument #1 ($array) must be of type array, null given in /Users/anthonyrebair/Local Sites/*******/app/public/wp-content/plugins/code-snippets/php/snippet-ops.php(582) : eval()'d code:11 Stack trace: #0 … : eval()'d code on line 11
@trebair I don’t get that error. Neither in the backend, nor in the builder or on the frontend. But just try to change the following line…
if ( is_admin() ) {
… to this…
if ( is_admin() || ! is_array( $value ) ) {
… and see if that fixes the error.
Great!! Fixed it!
Thank you for your solution!
Tony
Awesome. Glad I could help.