Skip Navigation

[Resolved] Query Filter from ACF Date field not working

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by Minesh 1 year, 5 months ago.

Assisted by: Minesh.

Author
Posts
#2709014
Screenshot 2024-07-23 151905.png
Screenshot 2024-07-23 151823.png

Hi Support,

We're using a query filter to filter posts that have an ACF custom field to select a date for an event. This isn't working as intended, we have it set to show posts that have a date greater than todays date. But the loop is pulling through ones with dates from April.

I've uploaded 2 screenshots. One of the ACF date field showning the field name etc. and the other is the query filter on Edit view.

hidden link is the URL where this view is being used. You'll see that there are 2 courses in April that shouldn't be showing.

Any ideas/help with making this right would be great.

I look forward to hearing from you.

Many Thanks

Chris

#2709203

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can I have admin access details and let me check what's going wrong with your setup.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2711350

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've added the following code to "Custom Code" section offered by Toolset with code snippet "custom_date":
=> hidden link

add_filter( 'wpv_filter_query', 'func_acf_date_field_date_filter', 10, 3);
function func_acf_date_field_date_filter( $view_args, $view_settings, $view_id ){
   
    if ( $view_id == 3393  ) {
   
            if ( (isset($view_args['meta_query'])) && (!empty($view_args['meta_query'])) ) {
     
             $target_field = "date_field"; // change this field slug to your field slug
                  
            foreach ($view_args['meta_query'] as $key => $value):
                       
              if ($value['key'] == $target_field){
 
                     $view_args['meta_query'][$key] = array('key' => $target_field, 
                                                       'value' => current_time('Ymd'), 
                                                       'type' => 'DATE', 
                                                       'compare' => '>' );
              }
            endforeach;            
        }
          
    }
   
    return $view_args;
}

More info:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset