Skip Navigation

[Gelöst] Date comparisons in conditional shortcodes

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

Problem:
How to conditionally display something if a custom date field value is more than one year ago.

Solution:
Use the wpv-conditional shortcode and add a condition that the date field plus the number of seconds in one year is less than today's date. (Dates are stored in UNIX timestamp format.)

For example, this will work for a custom field with a slug of due-date:

[wpv-conditional if="( $(wpcf-due-date) + 31536000 lt 'TODAY()' )"]
<p>Due date is more than one year ago</p>
[/wpv-conditional]

Pay careful attention to the formatting of the if condition.

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-types-fields-and-custom-fields/

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

Last updated by donaldM-2 vor 6 Jahre, 3 Monate.

Assisted by: Nigel.

Author
Artikel
#605318

I have a custom field called last paid. I would like to compare that date to the current date and if the difference is greater than a year, I would like to show a red circle. If it is less than a year, green dot. I can take care of the dots and styling but the code to show them I am having issues with.

Link here:

hidden link

I would like to show the dot on the single page for the profile maybe next to their name and on this page: hidden link as well. I am thinking next to their name as well to the right.

#605427

Nigel
Supporter

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

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

Hi Donald

I was testing the solution before proposing it to you, but it doesn't seem to be working as expected.

I'm just waiting for feedback from colleagues and then I'll update you again.

#605479

Nigel
Supporter

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

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

Hi Donald

So testing this has uncovered what looks like a bug with calculations in conditional shortcodes (we need to calculate today minus one year).

Rather than have you wait on a fix I have written a quick code snippet.

Add the following code to your theme's functions.php file or using a plugin such as Code Snippets:

function tssupp_within_year( $user_id ){

	if ( !isset( $user_id ) ) {
		$user_id = get_current_user_id();
	}

	$user_date_field = 'wpcf-last-paid'; // edit if required

	$user_date_value = get_user_meta( $user_id, $user_date_field, true );

	$one_year_ago = time() - 60*60*24*365;

	return ( $user_date_value > $one_year_ago );
}

You need to register this function to be able to use it inside conditional shortcodes at Toolset > Settings > Front-end Content

Now, I'm not sure how you have set up the member account page.

The function needs to know the ID of the user it will get the last-paid field from. By default if you don't specify a user ID it will assume the current logged-in user.

Then when you want to conditionally display some content you can do so like so:

          [wpv-conditional if="( 'tssupp_within_year( )' )"]
          <p>Green</p>
          [/wpv-conditional]
          [wpv-conditional if="( 'tssupp_within_year( )' )" evaluate="false"]
          <p>Red</p>
          [/wpv-conditional]

Note I haven't specified a user ID as an argument in the function.

Depending on how you have set up the member page you may need to use the wpv-user shortcode to output the ID of the required user, for example: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user

#605503

So that is great. Yes it won't be grabbing the user logged in than just associate the time check with the post it is on or associated to. I guess the user ID

#605525

Nigel
Supporter

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

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

The last-paid field is a user field not a post field.

How does the page in question know which user it is displaying data for? How do you have this set up?

#605526
Screenshot at Jan 11 13-00-20.png
Screen Shot 2018-01-11 at 12.59.26 PM.png

The last-paid is a custom field assigned to the post type members. When editing the post "member", the admin enters the last paid date for that member. See screenshot.

Then I show it in the content template for the single member. See screenshot.

I just need to compare it to the current date. Whether anyone is logged in or not.

#605649

Nigel
Supporter

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

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

Hi Donald

Sorry, I misunderstood. Because you were displaying a member profile I took it to me this was based on users, but you have a custom post type for it.

In which case we have the same problem with the bug in conditional shortcodes, and the code I suggested needs to be modified.

Try this instead:

function tssupp_within_year(){

	global $post;

	$date_field = 'wpcf-last-paid'; // edit if required

	$date_value = get_post_meta( $post->ID, $date_field, true );

	$one_year_ago = time() - 60*60*24*365;

	return ( $date_value > $one_year_ago );
}

You use it the same way as I described before, first registering the function for use inside conditional statements, and then adding something like the following wherever you want the red or green to appear in your content template for members.

[wpv-conditional if="( 'tssupp_within_year( )' )"]
<p>Green</p>
[/wpv-conditional]
[wpv-conditional if="( 'tssupp_within_year( )' )" evaluate="false"]
<p>Red</p>
[/wpv-conditional]
#605787

That worked like a charm! Thank you. Is there any way to make it work on the members list page too? hidden link

McGuinn,Donald should be green as well as Lehnen, Travis.

#605788

Sorry. My caching plugin wasn't showing it right! Everything works now! Thank you so much.

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