Skip Navigation

[Resolved] I am trying to set up a “date filter” (I have copied another ticket …)

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to setup a filter on custom date field in post view?

Solution:

You can follow our document to setup the date filter:

Relevant Documentation:

https://toolset.com/documentation/user-guides/views/date-filters/

This support ticket is created 2 years, 10 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by SJ5362 2 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#2324079

I am trying to have date filter here : hidden link (last select of the first line)

I have copied (and modified) another similar tickets (https://toolset.com/forums/topic/date-custom-field-range-filter/) but I am not successful.

Here is the snippet (I just changed the view_id to 38883 and the name of the field to "wpcf-date-debut"
------------
<?php
/**
* Ce code permet d'interpréter le filtre par date pour le calendrier interactif
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

add_filter( 'wpv_filter_query', 'func_custom_filter_query', 10, 3);
function func_custom_filter_query( $view_args, $view_settings, $view_id ){

if ( 38883 == $view_id ) {

if ( (isset($view_args['meta_query'])) && (!empty($view_args['meta_query'])) ) {

$target_field = "wpcf-date-debut"; // 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;
}
-----------
and here is the definition of the filter in toolset views :
[wpv-control-postmeta type="select" field="wpcf-date-debut" source="custom" values="1,2,3,4,5" display_values="Dans moins de 1 semaine,Dans moins de 1 mois,Dans moins de 3 mois,Dans moins de 6 mois,Dans moins de 1 an" default_label="Date" url_param="wpv-wpcf-date-debut"]

Thanks for your help! (as you can guess, I am not a PHP coder.....)

#2324545

Hello,

I have checked the the custom codes you mentioned above, it needs to add a filter on custom field "date-debut", you can edit the post view setup any filter on custom date field "date-debut" manually, and above custom codes will override it when user submit the search form.

If you need more assistance for it, please provide a test site with the same problem in below private message box, I need to debug it in a live website

#2325325

Sorry, I don't understand because I think that this was what I did ("add a filter on custom field "date-debut").

Isn't that what this part of the code is doing :
[wpv-control-postmeta type="select" field="wpcf-date-debut" source="custom" values="1,2,3,4,5" display_values="Dans moins de 1 semaine,Dans moins de 1 mois,Dans moins de 3 mois,Dans moins de 6 mois,Dans moins de 1 an" default_label="Date" url_param="wpv-wpcf-date-debut"]

?

#2325659

Nope, the shortcode you mentioned above can only output a dropdown in custom search form, but you still need to add a custom field filter to the post view, see our document:
https://toolset.com/documentation/user-guides/views/date-filters/

#2328867

My issue is resolved now. Thank you!