Hi,
I think I found the solution.
As mentions in my other reply, the way ACF stores the date in the database caused the issue. ACF stores the date field as YYYYMMDD
and the Date & Time field as Y-m-d H:i:s
.
If you add the php code below in your functions.php of your child theme (or custom plugin), the date will be stored as timestamp in the database.
After adding the php code, you need to edit every post with the date field en pick and save the date again, otherwise it will not work.
This loop shortcode is now working on my website:
[loop type=mycustomposttype field=myacfdatefield after='5 weeks ago' orderby=field_num key=myacfdatefield order=DESC]
[field title]
[/loop]
Use this php code when you are using the ACF DATE picker:
/**
* Convert values of ACF core date pickers from Ymd to timestamp
* @param string $value unmodified value
* @param int $post_id post ID
* @param object $field field object
* @return string modified value
*/
function acf_save_as_wp_date( $value, $post_id, $field ) {
if( $value ) {
$value = strtotime( $value );
}
return $value;
}
add_filter( 'acf/update_value/type=date_picker', 'acf_save_as_wp_date', 10, 3 );
/**
* Convert values of ACF core date pickers from timestamp to Ymd
* @param string $value unmodified value
* @param int $post_id post ID
* @param object $field field object
* @return string modified value
*/
function acf_load_as_acf_date( $value, $post_id, $field ) {
if( $value ) {
$value = date( 'Ymd', $value );
}
return $value;
}
add_filter( 'acf/load_value/type=date_picker', 'acf_load_as_acf_date', 10, 3 );
Use this code when you are using the ACF DATE & TIME picker:
/**
* Convert values of ACF core date time pickers from Y-m-d H:i:s to timestamp
* @param string $value unmodified value
* @param int $post_id post ID
* @param object $field field object
* @return string modified value
*/
function acf_save_as_timestamp( $value, $post_id, $field ) {
if( $value ) {
$value = strtotime( $value );
}
return $value;
}
add_filter( 'acf/update_value/type=date_time_picker', 'acf_save_as_timestamp', 10, 3 );
/**
* Convert values of ACF core date time pickers from timestamp to Y-m-d H:i:s
* @param string $value unmodified value
* @param int $post_id post ID
* @param object $field field object
* @return string modified value
*/
function acf_load_as_timestamp( $value, $post_id, $field ) {
if( $value ) {
$value = date( 'Y-m-d H:i:s', $value );
}
return $value;
}
add_filter( 'acf/load_value/type=date_time_picker', 'acf_load_as_timestamp', 10, 3 );
I hope it will also work for you!
Please let me know if it doesn’t work for you or if you run into a problem.
Peter
-
This reply was modified 5 years, 9 months ago by Peter Berger.