[Resolved] Date comparisons in conditional shortcodes
This thread is resolved. Here is a description of the problem and solution.
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.
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.
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:
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
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.
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.