I am trying to: see a list of team members with upcoming license expiration dates
Link to a page where the issue can be seen: the home page of the site
I expected to see: I have placed a date that should fire on team member Ronald Lindsay
Instead, I got: Only one ream member seems to be processed, the one from the previous view. The short code works. I pulled it into a test.php and ran it standalone. I think the problem may have something to do with my conditional:
<wpv-loop>
[wpv-conditional if="( '[sbf_license_expires license_expires_date="[types field="license-expires-date" style="text" format="Y-m-d"][/types]"]' gt '0' )"]
<li>
[wpv-post-link], [sbf_license_expires license_expires_date="[types field="license-expires-date" style="text" format="Y-m-d"][/types]"]
</li>
[/wpv-conditional]
</wpv-loop>
Note: the middle view is working fine - I duplicated the view and just changed the custom field from hore-date to license-expires.date. Here are the two shortcodes:
//***************************************************************
// return message if gaming license expires within 90 days - pass in license expires date
//***************************************************************
function sbf_license_expires($args=array()) {
$license_xdate = $args['license_expires_date'];
if(!$license_xdate){
// can't calculate
return -1;
} else
// make date objects
$today = date('Y-m-d');
$license_xdate_obj = date_create($license_xdate);
//start looking 90 days before expiration
$current_plus_90_date_obj = date_create($today);
date_add($current_plus_90_date_obj, date_interval_create_from_date_string("90 days"));
if(($license_xdate_obj <= $current_plus_90_date_obj)) {
$output = " license expires on ".$license_xdate_obj->format('m/d/Y');
} else {
//this normally returns -1. I changed it to see what was being processed.
$output = $license_xdate_obj->format('m/d/Y');
}
return $output;
} // sbf_return_license_expires
add_shortcode( 'sbf_license_expires', 'sbf_license_expires' );
//***************************************************************
// return length of service - pass in hire date
//***************************************************************
function sbf_return_los($args=array()) {
$hire_date = $args['hire_date'];
if(!$hire_date){
// can't calculate
return -1;
} else
// make date objects
$today = date('Y-m-d');
$hire_date_obj = date_create($hire_date);
// low date range - drop off after anniversary passes
$current_date_obj = date_create($today);
// high date range
$current_plus_45_date_obj = date_create($today);
date_add($current_plus_45_date_obj, date_interval_create_from_date_string("45 days"));
// create anniversary date from crrent year plus month and day of hire date
$anniversary_date = date_format($current_date_obj,"Y")."-".date_format($hire_date_obj,"m-d");
$anniversary_date_obj = date_create($anniversary_date);
if(($anniversary_date_obj >= $current_date_obj) && ($anniversary_date_obj <= $current_plus_45_date_obj)) {
$interval = date_diff($hire_date_obj, $anniversary_date_obj);
$y = $interval->format('%y');
if($y < 1) {
$output = -1;
} else {
$output .= $interval->format('%y');
$output .= $y > 1 ? " years " : " year ";
$output .= "on ".$anniversary_date_obj->format('m/d/Y');
}
} else {
$output = -1;
}
return $output;
} // sbf_return_los
add_shortcode( 'sbf_return_los', 'sbf_return_los' );
Okay first, using a conditional in a View like this is likely to cause problems with pagination. The preferred method is to apply a Query Filter to this View, or add a custom filter programmatically using the wpv_filter_query filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
If that's not an option for you, start with a syntax cleanup. I recommend using double quotes at the first level, then single quotes everywhere inside those double quotes:
[wpv-conditional if="( '[sbf_license_expires license_expires_date='[types field='license-expires-date' style='text' format='Y-m-d'][/types]']' gt '0' )"]
<li>
[wpv-post-link], [sbf_license_expires license_expires_date="[types field='license-expires-date' style='text' format='Y-m-d'][/types]"]
</li>
[/wpv-conditional]
If that doesn't resolve the issue, the next step is to add the debug attribute to the conditional like this:
[wpv-conditional if="( '[sbf_license_expires license_expires_date='[types field='license-expires-date' style='text' format='Y-m-d'][/types]']' gt '0' )" debug="true"]
<li>
[wpv-post-link], [sbf_license_expires license_expires_date="[types field='license-expires-date' style='text' format='Y-m-d'][/types]"]
</li>
[/wpv-conditional]
On the front-end of the site, you should see some debug information about the conditional. Copy + paste that here for me to review. If no debug information is displayed, then try to narrow down the problem further by testing each part of the conditional chain:
Date field: [types field='license-expires-date' style='text' format='Y-m-d'][/types]<br />
Custom shortcode with hard-coded date: [sbf_license_expires license_expires_date='2018-04-10']<br /> (change the date to something that makes sense for your site)
That should give you some information about each individual component so you can see if there's something obviously wrong.
I removed the conditional and the shortcode from the view and sorted the view on the license_expires_date. That works. What I want to see is all team members with licenses expiring within the next 90 days. I tried adding a query filter of MONTHS_FROM_NOW(3) and even tried just TODAY. As soon as I put the query filter in place I get no output. License_expires_date is defined as a date in custom fields. Am I using the query filter properly?
Try the settings as shown in the attached screenshot.
- UNSIGNED
- between
- TODAY
- FUTURE_DAY 90
Now that I understand the syntax, I can modify the range of dates easily. Thank you!