Skip Navigation

[Resolved] Conditional output related to date fields

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

Problem:

I have a CPT that include 2 dates, one start date, one end-date.
Displaying the output is pretty simple and easy, but I would love to do this :

IF month from start_date is different month from end_date, do something
IF month from start_date is equal month from end_date, do something else
So i think I first need to get the data out of the date fields to be able to compare the month and that is where I am stuck

Solution:

You can use Types shortcode to output the month value of custom date field by setting the "format" attribute, for example:
[types field="start_date" format="m"][/types]
[types field="end_date" format="m"][/types]
https://toolset.com/documentation/customizing-sites-using-php/functions/#date
format:
any valid WordPress date format

Then you can use them in the Views shortcode [wpv-conditional] to check if the month values are equal, for example:

[wpv-conditional if="( '[types field="start_date" format="m"][/types]' eq '[types field="end_date" format="m"][/types]' )"] 
do something
[/wpv-conditional]
[wpv-conditional if="( '[types field="start_date" format="m"][/types]' eq '[types field="end_date" format="m"][/types]' )" evaluate="false"] 
do something else
[/wpv-conditional]

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

50% of people find this useful.

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

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 6 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#606580

***Tell us what you are trying to do?***
Hi, I have a CPT that include 2 dates, one start date, one end-date.
Displaying the output is pretty simple and easy, but I would love to do this :

  • IF month from start_date is different month from end_date, do something
  • IF month from start_date is equal month from end_date, do something else

So i think I first need to get the data out of the date fields to be able to compare the month and that is where I am stuck

*** Is there any documentation that you are following? ***
None that i can found

*** What is the link to your site? ***
Sample in the left column
hidden link

*** THANK YOU FOR YOUR HELP ***

#606647

Dear Louis,

You can use Types shortcode to output the month value of custom date field by setting the "format" attribute, for example:
[types field="start_date" format="m"][/types]
[types field="end_date" format="m"][/types]
https://toolset.com/documentation/customizing-sites-using-php/functions/#date
format:
any valid WordPress date format

Then you can use them in the Views shortcode [wpv-conditional] to check if the month values are equal, for example:

[wpv-conditional if="( '[types field="start_date" format="m"][/types]' eq '[types field="end_date" format="m"][/types]' )"] 
do something
[/wpv-conditional]
[wpv-conditional if="( '[types field="start_date" format="m"][/types]' eq '[types field="end_date" format="m"][/types]' )" evaluate="false"] 
do something else
[/wpv-conditional]

https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional

#606811

I really thank you, I haven't seen the output of the date and it's pretty amazing what conditional can do when well implement.
For future work I will now remember that I can use shortcode in conditional output!

#606949

You are welcome