Skip Navigation

[Resolved] How to filter view by custom date field by day, days, month, year, week

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

Problem:
How to filter view by custom date field by day, days, month, year, week

Solution:
You need to use the view's API hoook 'wpv_filter_query' in order to filter your view results.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/date-filter-not-working/#post-1227116

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 5 years, 8 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 7 replies, has 2 voices.

Last updated by romanB-3 5 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#1226784

Hello,
I have set up a simple date filter based on a code snippet found here.

Here is my function

add_filter( 'wpv_filter_query', 'filter_postdate_func', 10, 3 );
function filter_postdate_func( $query, $settings, $views_id ) {
     
    if($views_id != 1727 || !isset($_GET['date-incident'][0]) || empty($_GET['date-incident'][0])) return $query;
     
    $query['date_query']['after'] = $_GET['date-incident'][0] . ' ago';
    return $query;
}

Then I have this custom filter in parametric search :

Sélectionner les éléments avec le champ : Date est un nombre supérieur ou égal à URL_PARAM(date-incident) \n 

"Date" is a custom field filled by the user.

But on frontend, the filter doesn't seem to work.

Thank you.

#1227015

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - first of all, I would like to clarify here that Types custom date field stores the date value as Unix timestamp.

That means, the "date_query" argument you are using will not work.

I need to know first here what posts you want to filter or and where exactly on which page you added the view?

#1227032

Thank you for your reply.

In deed this seems to be the problem. My function was based on a postdate function, and postdate seem not to be stored as date custom fields.

The custom filter is set up like this :

<div class="form-group">
  <label>[wpml-string context="wpv-views"]Depuis[/wpml-string]</label>
  	[wpv-control url_param="date-incident" type="select" values=",1095.75,730.5,365.25,182.63,91.31,30,14,7,3,2,1" display_values="2015,3 ans,2 ans,1 an,6 mois,3 mois,1 mois,2 semaines, 1 semaine, 72 heures, 48 heures, 24 heures"]
</div>

Could this new function work better in this case ?

// FONCTION DE FILTRE PAR DATE
add_filter( 'wpv_filter_query', 'filtre_dateincident', 10, 3 );
function filtre_dateincident( $query, $settings, $views_id ) {
     
    if($views_id != 1727 || !isset($_GET['date-incident'][0]) || empty($_GET['date-incident'][0])) return $query;
     
    $query['number']['superior'] = $_GET['date-incident'][0] * 24 * 60 * 60;
    return $query;
}

Could you please activate private fields in order I can share the view link ?
Thank you.

#1227091

Minesh
Supporter

Languages: English (English )

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

sorry - I would like to know for what field you added the filter, you want to apply the filter value to default post date - correct?

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

#1227092
#1227116

Minesh
Supporter

Languages: English (English )

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

ok - I see the date is a custom date field. now, I would like to know how you want to filter your post based on the selection.

I've adjusted the code as given under to your current theme's functions.php file:

add_filter( 'wpv_filter_query', 'func_filtre_dateincident', 10, 3 );
function func_filtre_dateincident( $query, $settings, $views_id ) {
      
    if($views_id == 1727){
      
	 if(!empty($query['meta_query'])){ 
       foreach((array)$query['meta_query'] as $k=>$v):
            if(isset($v['key']) and $v['key']=='wpcf-date'){
                  

				$values = $v['value'];
                if(!empty($values)){
					
                    unset($query['meta_query'][$k]);
                     
                        $query['meta_query'][$k]['key'] = 'wpcf-date';
                        $query['meta_query'][$k]['type'] = 'NUMERIC';
                        $query['meta_query'][$k]['compare'] = '>=';
                        $query['meta_query'][$k]['value'] = time() - $values * 24 * 60 * 60;
                }
				  
               
                     
            }
        endforeach;
	 }        
     
    return $query;
	}
}

Where:
- As you can see we are subtracting the selected time vlaue from current time().

Can you please confirm it works as expected at your end. 🙂

#1227170

This is really awesome, thank you very much !
Still, I have the strange feeling this has slowed down the view a lot ; is it possible ? or am I mistaking ?
Thank you.

#1227175

Minesh
Supporter

Languages: English (English )

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

Well - I'm glad that solution I shared help you to resolve your issue.

No, this filter isnt not that much heavy its totally depend on how much content you have, the number of filters added to views and your server resources. If you really track down the performance issue is from view please feel free to open a new ticket with your every new question.

More info:
=> https://toolset.com/documentation/user-guides/how-to-use-views-parametric-search-on-large-sites/

This will help other users searching on the forum as well as help us to write correct problem resolution summary for which originally ticket was submitted.

#1227180

My issue is resolved now. Thank you!