I have a url parameter which is filtering on a date field
I when to get it to filter on the date field but start from 14 days in the past and past that as the url _param
My view is this
[wpv-filter-start hide="false"]
[wpv-filter-controls]
<div>
<h4>Your Planner Events List</h4>
</div>
<div class="form-group">
<label>[wpml-string context="wpv-views"] Select Start Date[/wpml-string]</label>
[wpv-control-postmeta field="wpcf-stdstartdate" date_format="d/m/Y" default_date="PAST_DAY(14)" url_param="wpv-wpcf-stdstartdate"]
[wpv-filter-submit name="Search your list for events" output="bootstrap"]
<div class="pagination">
[wpv-pager-prev-page][wpml-string context="wpv-views"]Previous Page [/wpml-string][/wpv-pager-prev-page]
[wpv-pager-next-page ][wpml-string context="wpv-views"]Next Page[/wpml-string][/wpv-pager-next-page]
</div>
[/wpv-filter-controls]
[wpv-filter-end]
</div>
The default date is fine but that only sets a display date and you have to submit to get it to filter.
What I would like it to do is the filter by default from 14 days in the past, but still past that as the url_param
Hi Mike,
Thank you for contacting us and I'd be happy to assist.
To make sure that a similar filter for the past 14 days is also applied when the search form hasn't been submitted, you can use a custom function attached to the "wpv_filter_query" filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Example:
add_filter( 'wpv_filter_query', 'filter_by_date_func', 999, 3 );
function filter_by_date_func( $query_args, $view_settings, $view_id ) {
if ( ( $view_id == 1234 ) && ( !isset($query_args['meta_query']) ) ) {
$query_args['meta_query'][0]['key'] = 'wpcf-stdstartdate';
$query_args['meta_query'][0]['value'] = strtotime("-14 days");
$query_args['meta_query'][0]['type'] = 'NUMERIC';
$query_args['meta_query'][0]['compare'] = '>=';
$query_args['meta_query']['relation'] = 'AND';
}
return $query_args;
}
The above code can be added to the active theme's "functions.php" file and please replace "1234" with the actual ID of your view.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
My issue is resolved now. Thank you!