Skip Navigation

[Resolved] How to user past_day(int) as a url_parameter

This support ticket is created 5 years, 4 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/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by mikeB-14 5 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#1317797
filter past.JPG

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

#1317849

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

#1318837

My issue is resolved now. Thank you!