Skip Navigation

[Gelöst] How to use the post date field in a conditional shortcode

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
Client is using wpv-post-date in a conditional shortcode to display posts published in the last month, but it's not working as expected.

Solution:
The post date needs to be in the UNIX timestamp for such comparisons, which can be achieved with the format="U" attribute, like so:

[wpv-conditional if="( '[wpv-post-date format='U']' gt 'MONTHS_FROM_NOW(-1)' )"]
conditional content
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-date

This support ticket is created vor 5 Jahre, 11 Monate. 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 3 Antworten, has 2 Stimmen.

Last updated by Ido Angel vor 5 Jahre, 11 Monate.

Assisted by: Nigel.

Author
Artikel
#809653

hey,

i want to display a "new!" badge for products published in the last month.
i tried this:

[wpv-conditional if="( '[wpv-post-date]' lt 'MONTHS_FROM_NOW(-1)' )"]<span class="new"><img src="<em><u>hidden link</u></em>" width="54" height="60" class="size-full" /></span>[/wpv-conditional]

but that didn't work.

what am i doing wrong?

thanks!
ido

#810835

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

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

Hi Ido

I'm editing this reply with a simple solution pointed out by a colleague.

I have had problems with the relative date constants in Views filters before, but this should work fine in conditional shortcodes.

Use the format="U" attribute to convert the date to a timestamp and you will be able to compare it to the timestamp one month ago generated by MONTHS_FROM_NOW(-1)

[wpv-conditional if="( '[wpv-post-date format="U"]' lt 'MONTHS_FROM_NOW(-1)' )"]<span class="new"><img src="<em><u>hidden link</u></em>" width="54" height="60" class="size-full" /></span>[/wpv-conditional]
#813863

hey nigel,

thanks - but that didn't work.

i used:

[wpv-conditional if="( '[wpv-post-date format='U']' lt 'MONTHS_FROM_NOW(-1)' )"]<span class="new"><img src="<em><u>hidden link</u></em>" width="54" height="60" class="size-full" /></span>[/wpv-conditional]

and i'm not getting the image.

#832944

hey,
bumping this to say this is the right code:

[wpv-conditional if="( '[wpv-post-date format='U']' gt 'MONTHS_FROM_NOW(-1)' )"]<span class="new"><img src="<em><u>hidden link</u></em>" width="54" height="60" class="size-full" /></span>[/wpv-conditional]

(had to be GT and not LT 🙂
cheers!
ido

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