I need to only get results that are less than 2 years in the future. Normally I use the View filter option for this, but this time the custom date field is an ACF one. It stores the date as yyyymmdd and I think it's because of this the View filter doesn't work.
Is there a way to apply this filter to the View using a hook or filter?
It would be:
Only get products where the ACF field "publication_date" is less than 2 years in the future.
Thanks
Alex
Hi Alex,
Welcome to Toolset support.
Because the ACF date is stored as text yyyymmdd, you can't use the built-in "date is before X days" filter directly – it expects Toolset's own timestamp date field.
This will need custom coding, which is outside of our support scope. I can give you an initial suggestion on how to go forward, but please consider that we can not maintain the suggested code, and the usage of the code is at your discretion.
You can use wpv_filter_query and compare as a string against "today + 2 years" in the same format:
add_filter( 'wpv_filter_query', 'ts_filter_publication_date', 100, 3 );
function ts_filter_publication_date( $view_args, $view_settings, $view_id ) {
// Change 123 to your View ID
if ( $view_id == 123 ) {
$limit_date = date( 'Ymd', strtotime( '+2 years' ) );
$view_args['meta_key'] = 'publication_date'; // ACF field name
$view_args['meta_value'] = $limit_date;
$view_args['meta_compare'] = '<=';
$view_args['meta_type'] = 'CHAR';
}
return $view_args;
}
You will need to change 123 with the View ID that you have.
You will need to change publication_date with the ACF field name that you have.
For more information: (I got the code idea above from the link below)
https://toolset.com/de/forums/topic/view-query-filter-by-date-custom-field-not-working/
Official documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Thanks.