Skip Navigation

[Resolved] Custom Date View

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.

This topic contains 5 replies, has 2 voices.

Last updated by Waqar 2 months, 1 week ago.

Assigned support staff: Waqar.

Author
Posts
#2059029

Tell us what you are trying to do? Attempting to set up a view that will only show results from the past year based on a custom date field. When you filter the view no results are shown.

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site? hidden link

#2059463

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

Do you want to show the results from the past year with respect to today's date or does it need to be any post with last year's date (i.e. 2020)?

I'll be able to suggest the next steps accordingly.

regards,
Waqar

#2060267

Hello Waqar,

We are wanting results displayed based on a custom date field

#2060965

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

I understand that you'd like to show the results displayed based on a custom date field.

But, my question was more on the lines of the term 'past year'. Please choose which one of these two descriptions matches your requirement:

A. Today's date is '19 May 2021' and you'd like to show all results where the date custom field value is from '19 May 2020' to '19 May 2021'.

OR

B. The current year is '2021' and you'd like to show all results where the date custom field value is any date from the last year, that is '2020'.

#2062145

Thank you for the response! I apologize for not being more specific. We would like to display the current month and the previous 11 months.

#2064143

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for confirming.

This is a very specific requirement, for which you'll need to add a custom filtering function, using the 'wpv_filter_query' filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example:


add_filter( 'wpv_filter_query', 'wpv_filter_query_func', 1000 , 3 );
function wpv_filter_query_func( $query_args, $view_settings ) {
	// skip if blocks edit screen
	if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
		return $query_args;
	}
	// include IDs of the target views
	$target_views = array(1234);
	// add the custom field key to compare with
	$target_field_key = 'post-date-field';
	
	// process if specific view
	if ( !is_admin() && ( isset($view_settings['view_id']) && in_array($view_settings['view_id'], $target_views)) ) {

		$todayDate = new DateTime('now');
		$futureDate = $todayDate->modify('last day of this month');
		$futureDate = $futureDate->setTime(0,0);
		$maxRange = $futureDate->format('U');

		$todayDate = new DateTime('now');
		$date11MonthsAgo = $todayDate->modify("-11 months");
		$date11MonthsAgo = $date11MonthsAgo->modify('first day of this month');
		$date11MonthsAgo = $date11MonthsAgo->setTime(0,0);
		$minRange = $date11MonthsAgo->format('U');        

		$query_args['meta_query'][] = array(
			'key' 	=> 'wpcf-'.$target_field_key,
			'value'   => $minRange.','.$maxRange,
			'type'   => 'NUMERIC',
			'compare' => 'BETWEEN',
		);

	}
	return $query_args;
}

Note: Please replace '1234' and 'post-date-field' with your actual view's ID and the date type custom field's slug.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Please also make sure that you haven't added a query filter for this date field in the view's settings.

The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/