Skip Navigation

[Resolved] Custom Field Filter containing dates are compared to epoch datetime

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

Problem:
A View filter uses TODAY() for a date comparison, and it doesn't work because that generates a timestamp and the non-Types date field is stored in the mySQL format.

Solution:
If your custom field must be in a non-timestamp format then the solution is to use the wpv_filter_query hook to modify the query arguments so that they compare the dates in the same format.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 5 years, 8 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by patriceB-2 5 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#1128139
toolset-support-custom-field-filter-date-epoch.png

Hi,

I'm building an archive view with a custom field filter.

I'm comparing the custom field to 'TODAY', as shown in the attached file.

I always got no results in my view, so I opened the code and noticed the 'TODAY' sql statement used the epoch format ('timestamp').

My question:

Do custom fields containing dates must be formatted with epoch format, rather than standard WordPress ('mysql') format ?

#1128217

Nigel
Supporter

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

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

Hi Patrice

Types date fields are stored as UNIX timestamps, hence when you add a date Query Filter it expects to use a timestamp for the comparison, which is what the functions such as TODAY() will generate. (And as timestamps are numbers, the comparison should be NUMBER.)

If you are using a non-Types field where the date is not stored as a timestamp, you should use the wpv_filter_query hook and manually set up the query arguments as required.

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#1128220

Brilliant. I should have thought of it !

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.