Skip Navigation

[Resolved] Filter By Date in The View

This support ticket is created 4 years, 12 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 2 voices.

Last updated by Nigel 4 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1383045

Tell us what you are trying to do?
I am trying to add a date filter to my view here hidden link
I need the date filter to be flexible so that the user doesn't have to choose all the date values from the date picker (year, month, day). I want the search to work even with one value it is either (year, month, day).
For now, my search needs the three values to be entered in order to work.

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?
hidden link

Do you have any suggestions about this case?

#1383185

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

What you want isn't really possible.

Date custom fields are stored as timestamps, which is a point in time (specifically the number of seconds since 1 Jan 1970 00:00:00).

So as I write this, the current timestamp is 1573737209.

It is possible to convert the timestamp into a human readable format for display where you can see the year, the month, the day of the month, but they cannot be used to filter posts.

To be able to filter such fields in that way would require you to write your own custom SQL queries.

If you want to be able to filter by year, by month, and by day of the month separately, then they need to be separate fields, so instead of a date field create a year select field (which includes all the years in the expected range), a month select field (which includes the 12 months), and a day-of-month field which includes 1-31.

The users on the front end would then be able to filter by year, by month, by day-of-month, or by any combination of the three.

#1383225

Hello,
Thanks for your reply.
I have another suggestion and I need to know whether it is possible or not.
The suggestion is to keep only the existing dates active on the date picker, so the user can only click on the available dates to search with.

#1383311

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

That wouldn't work, either.

You can modify the options of the datepicker, and you can use these to limit the range of dates that can be selected from, for example (hidden link), but you can't specify individual dates that can be chosen from.

You could produce a custom solution using Views that achieved this, but it will require some PHP and JavaScript programming.

If you are able to do that, great, otherwise you'll need to find a developer to do that for you (such as can be found at toolset.com/contractors).

The first part is to modify how the filter control is inserted, so that instead of a datepicker you have a select dropdown.

Edit the Search and Pagination of your View. It will have a shortcode wpv-control-postmeta which inserts the datepicker. You can change this to a text input, but not a select, which is what you will need.

So edit that shortcode so that it is a more generic wpv-control shortcode that allows you to create your own filter controls, so that it looks something like this:

[wpv-control field="wpcf-my-date-field" type="select" url_param="wpv-wpcf-my-date-field" values="," display_values=","]

Note that your field attribute will be different (depending on the name of your date field), as well as the url_param attribute.

We specify a select field, and then we provide some options using the values and display_values attributes. I entered a comma for those just so that the select field would be rendered with one empty value corresponding to nothing selected. You will need to provide the actual values with a combination of PHP and JS.

The PHP will be required to query the posts and get the date field values for each post. You will then want to construct an array of these values.

You can then pass this array to the front-end for use by some custom JavaScript using the function wp_localize_script (https://developer.wordpress.org/reference/functions/wp_localize_script/, see here for a tutorial: hidden link).

Your JavaScript can then take this array and use it to build the options for the select input, where the value is the timestamp and which displays a human readable version of the date.

You would add that with the DOM ready event, but you would also need to re-build the select dropdown every time the filters or search is updated, for which you can use custom JavaScript front-end events that you can find placeholder code for using the Frontend events button in the custom JS editor of the Search and Pagination section of the View.

So, if you are familiar with PHP and JS you should be able to see how this is possible, but it is very much a custom solution.

#1385863

Thanks for your reply.
Also, thank you for the explanation for an alternative solution.
I will see what I can do.

#1385915

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, good luck!

Let me know how you get on.