Skip Navigation

[Resolved] Filter by date from today to 90 days from now

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

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

Problem: I would like to filter a View based on a date custom field. Only results between today and 90 days from now should appear.

Solution: Do not use conditional HTML inside the results of a View if you intend to use pagination. Instead, the preferred method is to apply a Query Filter. The settings for the date range filter you described are "UNSIGNED", "between", "TODAY", and "FUTURE_DAY 90"

Relevant Documentation:
https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/

This support ticket is created 5 years, 12 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by MikeT458 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#631727

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' );
#632117

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.

#632161

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?

#636843
Screen Shot 2018-04-08 at 11.51.02 AM.png

Try the settings as shown in the attached screenshot.
- UNSIGNED
- between
- TODAY
- FUTURE_DAY 90

#637467

Now that I understand the syntax, I can modify the range of dates easily. Thank you!

The forum ‘Types Community Support’ is closed to new topics and replies.

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