Skip Navigation

[Waiting for user feedback] I need to filter a View by date using an ACF field

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 1 voice.

Last updated by Christopher Amirian 1 day, 9 hours ago.

Assisted by: Christopher Amirian.

Author
Posts
#2839379

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

#2839493

Christopher Amirian
Supporter

Languages: English (English )

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.