Skip Navigation

[Resolved] Default query filter and custom parametric search for the same field

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

Problem: I have a custom search View and I would like to specify a default value of one or more filters.

Solution: The only way to set a default value for a custom search filter that responds to URL parameters is to use a URL parameter. Link to the search View page using the correct URL parameter to predefine one of the filter values. There is no JavaScript API for manipulating filter values on the front-end.

This support ticket is created 6 years, 5 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1074929

Hello,

I have created a custom content type Event with custom fields: 'date from' and 'date to', so the user can filter the events in a chosen time period. I was able to create this view, but I would like also to display only the upcoming events.

I understand I would need to set up the query filter for the field 'date from' is greater or equal TODAY() but I can't use anymore the 'date from' field, as I have already used it for parametric search.

How can I create a view that displays upcoming events with available for the user the 'date from' filter, so he can change the date and see past event?

Best regards,
Damian

#1075161

Hi, the only built-in way to set a default value for a parametric search filter in a custom search View is to use URL parameters. This means you would have to link to the page containing the View with the URL parameters already defined, like

<a href="<em><u>hidden link</u></em>">Go to Custom Search Page</a>

Unfortunately we do not have a JavaScript API available for filter manipulation, so I'm not able to provide any assistance manually controlling those filters beyond using URL parameters. We do offer wpv_filter_query and wpv_filter_query_post_process to manipulate database query criteria programmatically. More information about those APIs is available here:
https://toolset.com/documentation/programmer-reference/views-filters/

#1077537

Thank you Christian, I could add the parameter and function Today() to the URL(?wpv-wpcf-data-wydarzenia=TODAY()), but the reset button would link again to the link without parameters, the same if the user would change the filter or remove the date from parametric search, so it's unfortunately not a good workaround.

What if I could create a second hidden event date field that would copy the value from the first one, something like: event_date and event_date_helper and second take the value from the first one and it's hidden for the user. I could use later this second field to filter the query.

Is it possible to achieve it with Types plugin without much coding?

Best,
Damian

#1077587

but the reset button would link again to the link without parameters
Could you create a reset button or reset link that links to the correct URL? This will effectively reset the form, and I think it will be easier than manipulating filters with JavaScript. Something like:

<a href="/custom-search-page?wpv-start=123456789">Reset search</a>

What if I could create a second hidden event date field that would copy the value from the first one, something like: event_date and event_date_helper and second take the value from the first one and it's hidden for the user. I could use later this second field to filter the query.
This is difficult if your View is updated with AJAX, or if your View uses the "only show available options for inputs" feature. Complex custom JavaScript is required in these cases to keep your front-end filter in sync with the back-end filter, since they must contain different values in some cases. For simple Views that trigger page refreshes and always show all options, you might be able to do this with the wpv_filter_query API.