Skip Navigation

[Resolved] Date based search filter using checkboxes

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
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)

Author
Posts
#1347237

Hi,

My custom Post Types are events that have a start date (wpcf-start-date) and end date (wpcf-end-date).

On my archive view, I have built a search to filter the various custom fields but want to include the ability to search events that are: "upcoming", "live", or have "ended".

This has led me to believe I need to build a filter (add_filter) but I am unable to find code examples that match what I am trying to do...

__
Search
<input type="checkbox" value="upcoming"> Upcoming<br>
<input type="checkbox" value="live"> Live<br>
<input type="checkbox" value="ended"> Ended"<br>

Results
Upcoming: Show items where wpcf-start-date is greater than TODAY()
Live: Show Items where wpcf-start-date is lower than or equal TODAY() AND wpcf-end-date is greater than or equal TODAY()
Ended: Show items where wpcf-end-date lower than TODAY()
__

Any help you can give would be appreciated.

#1347439

Hello, it sounds like you need to add some predefined date range filters in a custom search View. Unfortunately nothing exactly like this exists in the current software, so custom code would be required. You should also keep these things in mind:
- There is no JavaScript API for custom search input fields, so AJAX updates in the filters and in the search results will not work as expected. You should disable AJAX pagination and search. Also the option "show only available options for each input" in the Custom Search panel under the "Let me choose individual settings manually" option should not be active.
- Since you plan to use other search filters too, you must combine those existing query filter criteria with the custom date range criteria. This could be more or less of a challenge depending on your level of PHP knowledge.
- You can add your own checkbox inputs in the View's Search and Pagination panel, for instance I changed the values and added the "name" attribute to the checkboxes code you shared here:

<input type="checkbox" name="date-range-1" value="1"> Upcoming<br>
<input type="checkbox" name="date-range-2" value="1"> Live<br>
<input type="checkbox" name="date-range-3" value="1"> Ended"<br>

Then you can use the PHP API wpv_filter_query to manipulate the View query programmatically based on those selected checkboxes. We have more information about this and other PHP APIs available here: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
In the wpv_filter_query callback, you will be able to determine which of the checkboxes was checked by testing to see if a corresponding key exists in the $_POST superglobal:

// set $range2 to be true or false depending on whether or not the checkbox was checked in the search filters
$range2 = isset( $_POST['date-range-2'] );

If this looks like a bit more than you can handle, we have a portal available where you can connect with skilled professionals: https://toolset.com/contractors
For smaller projects I would also consider https://codeable.io/developers/toolset/