Show content by date-field.
-
Is it possible to filter content by date (and to use today’s date for the comparison)?
The content type I’ve set up (for events) has a data-field and I’d like to be able to show a list of content in the future, as well as past events.
-
Hello,
Currently, it’s not possible to query posts by custom date field, but I can try to add that functionality in the next update. I’ll start looking at how to implement it.
Is your date field stored as a timestamp or formatted date? It would be easiest if it’s a timestamp.
So, you’d like to query by the date field, by comparing if it’s before or after current date. Is that correct?
OK, I added the feature to query by date field. Please see the latest update.
It’s best if the date is stored as a timestamp.
For example, to display future events:
[loop type="events" field="event_date" compare=">" value="now"]
You can see the documentation under Settings -> Custom Content-> Loop.
There’s also a way to display a timestamp formatted to date:
[field event_date in="timestamp" date_format="Y-m-d"]
I’m using this loop:
[loop type="news" field="highlight_until" value="past" in="string"][field highlight_until][/loop]
It returns: 2015-01-24
If I change ‘past’ in ‘future’ in this loopt, it still returns: 2015-01-24
I looked into this, and currently the parameter values past and future expect the field to contain both date and time. Hmm, since it’s a date field, it would make sense to not include time..
For now, could you try this?
[loop type="news" field="highlight_until" value="today" compare="<" in="string"]
This should work also:
[loop type="news" field="highlight_until" value="past" date_format="Y-m-d"]
I’ll think about how to simplify this.
It still returns 2015-01-24 in both cases.
The date is an ACF field by the way with a format yy-mm-dd.I’ve also tried to format the ACF field with an @ (unix timestamp), but that was converted in the loop to 1984 something…
I just made an update, so when the date field is stored as a string, the default format assumed is “Y-m-d” (i.e., 2015-01-11) and doesn’t include time. This is a different behavior than before, so I hope that no one will be affected by it.
Perhaps what’s wrong is the date format: according to the Codex here, “y” would give only the last two digits of the year, so “yy” would be 1515. Could you try storing the field as “Y-m-d”?
yy-mm-dd is the ‘highlight_until’ in the loop and is actually shown as 2015-01-24 by your plugin; not as 1515. It’s standard Jquery format for a datepicker used by ACF (yy – year (four digit))
If I use Y-m-d in ACF the output is Y-1-25
After the update still the same issue.I looked into this deeper. ACF stores the date as Ymd (PHP format) regardless of the option chosen. In the latest update, I added a parameter to simplify this:
[loop acf_date="highlight_until" value="past"]
This is equivalent to:
[loop acf_date="highlight_until" value="past" in="string" date_format="Ymd"]
It is confusing that the date format chosen in the ACF settings is JS format, and the stored format is “Ymd” (20150125) regardless.
Also, to display a ACF date field with the selected formatting, I added the same parameter for [field]. This gets the field using the ACF function so the formatting filter is applied.
[fied acf_date="highlight_until"]
Thanx alot for all the effort. I’m seeing some change, but it’s not working correctly.
The first loop you mention:[loop acf_date="highlight_until" value="past"][field acf_date="highlight-until"][/loop]
always shows 2015-01-25 (value = past or value = future).
The second loop is kinda working:[loop acf_date="highlight_until" value="past"][field acf_date="highlight-until"][/loop]
it is giving a different result for future and past, but the turningpoint is not today, but in the future. So 2015-01-25 is visible in the loop if I use ‘past’. 2017-01-25 is visible with ‘future’.
How strange.. Just curious, what format is the date displayed as a normal field?
[field highlight-until]
On my end, I see this format: 20140116. Are you getting 2014-01-25, with the hyphens included? It seems there could be a mismatch in date formats, that [loop] is comparing today 20140116 with dates stored in the format 2014-01-25. That might also explain why “the turning point is not today” when using value future or past. Could you try setting date_format=”Y-m-d” for the loop?
The field, in my case, is stored as 2015-01-25 with the hyphens. The addition of date_format=”Y-m-d” does the trick! It’s working as is should, thanx a lot.
In ACF the date can be stored as yymmdd, yy-mm-dd, yy/mm/dd etc. Adding date_format=”” is needed, I guess, to understand the choosen format in ACF.
Great, I’m glad we figured it out. I’ll note the point about date format in the documentation, and see if I can simplify it any further.
- The topic ‘Show content by date-field.’ is closed to new replies.