Skip Navigation

[Resolved] date custom field range filter

This support ticket is created 3 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

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 15 replies, has 3 voices.

Last updated by Jason 3 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#2197199

Tell us what you are trying to do?

I have a custom date field for an event's application deadline. I'd like to have a dropdown that allows users to filter posts based on how close events are to these deadlines. So something like:

Application Deadline
- select deadline -
in less than one week
in less than one month
in less than three months
in less than six months
in less than one year

Is there any documentation that you are following? no

Is there a similar example that we can see?

This site has pretty much the same filter: hidden link

What is the link to your site? hidden link

#2197569

Hello and thank you for contacting Toolset support.

This could definitely be implemented, but it may need some custom code.

First, the dropdown needs to be built manually. If you are using the blocks, you can use an HTML block for this dropdown.

Then, it will require custom code to implement the condition on the underlying wp_query. If this is about a view, you can use the wpv_filter_query hook. If this is about an archive template, you will use the pre_get_posts from WordPress
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
- https://developer.wordpress.org/reference/hooks/pre_get_posts/

You can check the possible date query arguments here https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

Keep in mind that Toolset date fields are stored as timestamps, the number of seconds from 1/1/1970. So, the comparison should be done as UNASSIGNED instead of a STRING.

You can find examples in the forum by scoping search like this hidden link

I hope this helps. Let me know if you have any questions.

#2199013

Hi Jamal,

I am trying to add this feature to a view that generates what exists on the link to my site from above. I have been searching support for similar implementations, but either am not understanding the answer or can't find it.

Could you give me more of a hint? Do you see an example at hidden link that matches or is similar to what I'm trying to do?

This post seems promising but I'm not sure. https://toolset.com/forums/topic/filter-by-custom-selected-date-field-to-100-days-in-future-from-that-date/

It seems to indicate that it might not work since I already have other filters in my view...

#2200643

Minesh
Supporter

Languages: English (English )

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

Jamal is on Vacation. This is Minesh here and I'll take care of this ticket. Hope this is OK.

It will require to setup the date filter with custom values and it will also require bit custom code to filter view using the "wpv_filter_query" hook.

Can you please share admin access details and I'll try to setup for you.

*** 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.

#2200875

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've added the following filter to your view's "Search and Pagination" box:
=> hidden link

<div class="form-group">
	<label for="wpv-wpcf-ts-event-date">[wpml-string context="wpv-views"]Event Date[/wpml-string]</label>
	[wpv-control-postmeta type="select" field="wpcf-ts-event-date" source="custom" values="0,1,2,3,4,5" display_values="Please Select,in less than one week,in less than one month,in less than three months,in less than six months,in less than one year" url_param="wpv-wpcf-ts-event-date"]
</div>

I've also add submit button as AJAX filter will not work as appropriate.

Then, I've added the following code to the following code snippet:
=> hidden link

add_filter( 'wpv_filter_query', 'func_custom_filter_query', 10, 3);
function func_custom_filter_query( $view_args, $view_settings, $view_id ){
 
    if ( 242 == $view_id ) {
 
         	if ( (isset($view_args['meta_query'])) && (!empty($view_args['meta_query'])) ) {
   
             $target_field = "wpcf-ts-event-date"; // change this field slug to your field slug
				
			foreach ($view_args['meta_query'] as $key => $value):
                                       if ($value['key'] == $target_field){
                                          
                                          
                                              $option =   $value['value'];
										   	  $start = time();
										   	  if($option==1){
												$end = strtotime("+7 day",$start);
											  }else if($option==2){
												  $end = strtotime("+1 month",$start);
											  }else if($option==3){
												  $end = strtotime("+3 month",$start);
											  }else if($option==4){
												  $end = strtotime("+6 month",$start);
											  }else if($option==5){
												  $end = strtotime("+12 month",$start);
											  }else if($option==0){
												  	unset($view_args['meta_query'][$key]);
												    return $view_args;
												  
											  }
                                             
                                             $filer_index = $key;
                                          
                 				}
            		endforeach;
              
   
            if ( isset($filer_index) ) {
                   
                $view_args['meta_query'][$filer_index] = array('key' => $target_field, 
                                                                  'value' => array($start, $end), 
                                                                  'type' => 'NUMERIC', 
                                                                  'compare' => 'BETWEEN' );
            }
			
             
          
        }
		
    }
 
    return $view_args;
}

Can you please confirm it works as expected.

#2200897

Hi Minesh, thank you for your help, this new filter seems to work and thank you for the detailed explanation on how you did it.

Is it possible to make this filter use ajax or is this just not a possibility with toolset?

#2201553

Minesh
Supporter

Languages: English (English )

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

Ajax filter is not possible at the moment. It will again require much custom code and investigation that is actually beyond the scope of our support policy. Happy to share you workaround that you can use instead and that is what I already shared with you, that it will require submit button to filter the view.

#2201623

Hi Minesh, thank you for the workaround and good to know ajax is possible, but that it will take additional help from a contractor. Issue resolved.

#2201699

Hi Minesh, I apologize, the date custom field you used to sort was different than the one I would like to use. I thought I could just update the spots where this field was mentioned, but it does not work. I changed this:

             $target_field = "wpcf-ts-event-date"; // change this field slug to your field slug

to this:

             $target_field = "wpcf-ts-app-deadline"; // change this field slug to your field slug

And this:

<div class="form-group">
    <label for="wpv-wpcf-ts-event-date">[wpml-string context="wpv-views"]Event Date[/wpml-string]</label>
    [wpv-control-postmeta type="select" field="wpcf-ts-event-date" source="custom" values="0,1,2,3,4,5" display_values="Please Select,in less than one week,in less than one month,in less than three months,in less than six months,in less than one year" url_param="wpv-wpcf-ts-event-date"]
</div>

to this:

<div class="form-group">
	<label for="wpv-wpcf-ts-app-deadline">[wpml-string context="wpv-views"]Application Deadline[/wpml-string]</label>
	[wpv-control-postmeta type="select" field="wpcf-ts-app-deadline" source="custom" values="0,1,2,3,4,5" display_values="Please Select,in less than one week,in less than one month,in less than three months,in less than six months,in less than one year" url_param="wpv-wpcf-ts-app-deadline"]
</div>

Can you tell me what else I would need to update to switch to another custom date field? Is it part of this?

add_filter( 'wpv_filter_query', 'func_custom_filter_query', 10, 3);
#2201701

Hi Minesh, I apologize, the date custom field you used to sort was different than the one I would like to use. I thought I could just update the spots where this field was mentioned, but it does not work. I changed this:

             $target_field = "wpcf-ts-event-date"; // change this field slug to your field slug

to this:

             $target_field = "wpcf-ts-app-deadline"; // change this field slug to your field slug

And this:

<div class="form-group">
    <label for="wpv-wpcf-ts-event-date">[wpml-string context="wpv-views"]Event Date[/wpml-string]</label>
    [wpv-control-postmeta type="select" field="wpcf-ts-event-date" source="custom" values="0,1,2,3,4,5" display_values="Please Select,in less than one week,in less than one month,in less than three months,in less than six months,in less than one year" url_param="wpv-wpcf-ts-event-date"]
</div>

to this:

<div class="form-group">
	<label for="wpv-wpcf-ts-app-deadline">[wpml-string context="wpv-views"]Application Deadline[/wpml-string]</label>
	[wpv-control-postmeta type="select" field="wpcf-ts-app-deadline" source="custom" values="0,1,2,3,4,5" display_values="Please Select,in less than one week,in less than one month,in less than three months,in less than six months,in less than one year" url_param="wpv-wpcf-ts-app-deadline"]
</div>

Can you tell me what else I would need to update to switch to another custom date field? Is it part of this?

add_filter( 'wpv_filter_query', 'func_custom_filter_query', 10, 3);
#2201703

Hi Minesh, I apologize, the date custom field you used to sort was different than the one I would like to use. I thought I could just update the spots where this field was mentioned, but it does not work. I changed this:

             $target_field = "wpcf-ts-event-date"; // change this field slug to your field slug

to this:

             $target_field = "wpcf-ts-app-deadline"; // change this field slug to your field slug

And this:

<div class="form-group">
    <label for="wpv-wpcf-ts-event-date">[wpml-string context="wpv-views"]Event Date[/wpml-string]</label>
    [wpv-control-postmeta type="select" field="wpcf-ts-event-date" source="custom" values="0,1,2,3,4,5" display_values="Please Select,in less than one week,in less than one month,in less than three months,in less than six months,in less than one year" url_param="wpv-wpcf-ts-event-date"]
</div>

to this:

<div class="form-group">
	<label for="wpv-wpcf-ts-app-deadline">[wpml-string context="wpv-views"]Application Deadline[/wpml-string]</label>
	[wpv-control-postmeta type="select" field="wpcf-ts-app-deadline" source="custom" values="0,1,2,3,4,5" display_values="Please Select,in less than one week,in less than one month,in less than three months,in less than six months,in less than one year" url_param="wpv-wpcf-ts-app-deadline"]
</div>

Can you tell me what else I would need to update to switch to another custom date field? Is it part of this?

add_filter( 'wpv_filter_query', 'func_custom_filter_query', 10, 3);
#2201715

Minesh
Supporter

Languages: English (English )

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

You should not touch this:

add_filter( 'wpv_filter_query', 'func_custom_filter_query', 10, 3);

Yes - those are the two parts.

As the ticket was resolved, system automatically delete the login information. Can you please share access details again.

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

#2202415

Minesh
Supporter

Languages: English (English )

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

Can you please check now, It should work OK.

I've set the option "Always show all values for inputs" from the "Custom Search Settings" section.

#2202813

Hi Minesh, thank you. Now when I select "in less than one year" (a filter that will probably produce more than one page of results?), I scroll to the bottom of the page and it acts like it will be loading more results, but then clears the page with "No items found"

#2202819

Minesh
Supporter

Languages: English (English )

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

You will require to switch to the manual pagination as you know we are not using ajax filter anymore.

I've set the pagination option to "Pagination enabled with manual transition and page reload". You can add pagination links as per your requirement.

More info:
=> https://toolset.com/documentation/legacy-features/views-plugin/views-pagination/