Skip Navigation

[Resolved] Filtering a VIEW by 2 custom date fields using AND

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

Problem:
How can I filter a Posts View by a Date Field, where the Date should be between Date A AND Datae B?

Solution:
You can do that with a Custom Filter or a Query Filter.

In both cases you need to compare against a NUMBER, if you use a Types Date Fields, and against a DATE if you use the WordPress native Post Date.

This is becuase Types stores the Dates as a Timestamp, which is a NUMBER, and WordPress does not store a Timestamp, therefore here you use Date.

Relevant Documentation:
https://toolset.com/documentation/user-guides/filtering-views-query-by-date/
https://toolset.com/documentation/user-guides/date-filters/

This support ticket is created 7 years, 9 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by williamM-3 7 years, 8 months ago.

Assisted by: Beda.

Author
Posts
#422443
filering-cpt-for-two-custom-date-fields.jpg

I have 2 custom date fields, "Next Inspection" and "First Service Date".

I want to create a filter in my VIEW that will filter this CPT where the "Next Inspection" is older than TODAY AND where "First Service Date" is not blank. (It must have a First Service date. The value can not be empty).

In a separate content template I am using in another part of the site, I am able to do write that like this :

[wpv-conditional if="( $(wpcf-date-of-next-inspection) lt 'TODAY()' ) AND ( NOT(empty($(wpcf-first-service-date))) )"]

But how can I achieve this same outcome when using the VIEW filter?

I was successfully doing 1 part of this, by checking that the Next Inspection Date is older than TODAY, but I don't know how to accurately add the second part to check if the First Service Date cannot be blank.

I have attached a screenshot as well for more clarity.

#422641

Have you tried "0"?

Since the Date Field (first inspection) is either NOT saved or is ANY date that is surely greater then "0", this should work.

So your filter would be "is a number different from 0".

This would return all dates, which means, all posts where the date feild is actually saved, means, not empty.

Let me know how that works out for you.

#423269
date-filtering.jpg

Thanks Beda for your support.

I was not sure how the value is stored from toolset if it is empty. So, I wasn't clear if toolset stores it as "0" or NULL, etc.

To be clear, I am checking that "First Service Date" field is NOT empty as one of the requirements.

I think I have this working correctly, but to confirm, for this field I should be filtering with "Constant" is that correct?

So it filtering by:

"Select items with field: Next Inspection Date is a number lower than TODAY()
AND
First Service Date is a number different from 0"

Please see the screenshot, I believe this is filtering with my desired result.

#423368

The thing is, for Fields where you enter no value, WordPress will NOT save anything to the Database.

That is why I suggest to use "0".

See, IF you save a Date, it WILL be greater then "0".
So you can be sure, using this, you will get posts only if they actually have a Date Saved!

You could actually use any value there, as it would NOT exist, in any case of a not saved date Field.

I hope I did not confuse you.
The approach you use is correct.

#426781

Thanks for the help. I have the view working as desired.

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