Skip Navigation

[Resolved] SELECT MAX(thedatefield)

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.

Our next available supporter will start replying to tickets in about 2.47 hours from now. 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+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by katjaL 5 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#1220025

Hi,
I need to show only the posts that have the latest date of the custom date field. (This is not the publish date of the post.)

In the old site this was queried with SELECT MAX(thedatefield). In Toolset there are many date parameters, but it seems there is no such as "select the biggest date" - and since I don't know PHP I don't quite know how to achieve this.

Perhaps I should
A. create a shortcode, say [LATESTDATE], which returns the latest date of the posts
B. filter posts where date = [LATESTDATE]

Or what would be your recommendation?

#1220253

Nigel
Supporter

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

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

Hi Katja

Types date fields are stored as timestamps, which are numbers, so the bigger the number the later the date.

You can create a View to display posts and ORDER the post by this date field as a number, descending, which will show the most recent posts first.

You could then add a LIMIT of 1, and this would then show just the latest post.

No coding required, you can do that in the View settings.

Is that what you need to do?

#1220288

Hi Nigel, thank you. Sorry for being unclear. I don't need to show the latest posts. I need to
1. first know what is the latest date of this certain custom date field of all the custom posts
2. then show all the posts that have this same date.

The custom date field is not the post creation or last update field. It's kind of a "decision date". So there have been made some important decisions on certain date and I want to show all the decisions that were made last time.

#1220382

Nigel
Supporter

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

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

But it is still the last of these dates that you want to display posts for, right?

So the solution I described above would work (ordering the posts by this custom date field rather than the post_date or post_modified_date fields), except the problem is you don't know how many posts to display, there could be 1 or there could be six.

You can still do this without coding.

You create a View as described, and in the output section use the Loop Wizard and choose the List with separators format, and only output the raw date field value (e.g. use wpv-post-field shortcode to output it, or add the output='raw' attribute to a types shortcode).

So, this View will simply output the date (in timestamp format) of the custom date field that represents the latest of such dates.

Now you need a second View, similar to the first but without the limit or order options, which includes a Query Filter for the date field where the value comes from a shortcode attribute. When you insert the View you then add such a shortcode attribute, where its value comes from your first View.

Then the second View will output all of the posts which have the same custom date field value as the first View (which shows the last of such dates).

Does that make sense?

#1221045

Hi Nigel, sounds like a very clever approach, but I can't make it work.

I have the first view ready. In the second view I have this query:

Select items with field:
Päätöspäivämäärä is a NUMBER equal to VIEW_PARAM(uusintp)

The "päätöspäivämäärä" is the date field. So what should the VIEW_PARAM be? This "uusintp" is the first view's name, but it doesn't work. And instead of NUMBER I've tried the other options too.

#1221412

Nigel
Supporter

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

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

Hi Katja

Can you check that the first View is outputting what's expected, namely the relevant date as a timestamp (e.g. 1551398400 would be 1 March 2019).

If your second View expects a parameter "uusintp" for the value to filter by then that is what you provide, like so:

[wpv-view name="view-2-name" uusintp="[wpv-view name='view-1-name']"]

Be careful with the single and double quotes, as I have above.

NUMBER is the correct option to use.

#1221522

YES! Thank you Nigel very much of great support 🙂