Skip Navigation

[Resolved] Date Ranger Filters and Pagination

This support ticket is created 4 years, 1 month ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by jannaH 4 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#1878635

Tell us what you are trying to do?
I am trying to create a custom view of a specific post type and creating filters users can use to filter the posts. hidden link

However, i can't seem to get the date rate to find any results And I can't get the pagination to show.

Is there any documentation that you are following?
No

Is there a similar example that we can see?
This page using Facet WP is what I would like to mimic and replace/eliminate Facet WP hidden link This has the date range and pagination.

What is the link to your site?
hidden link
I can provide the code or access if needed.

#1879101

Hi,

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

To troubleshoot this, I'll need to see how these fields and the search filters are set up in the admin area.

Can you please share temporary admin login details, in reply to this message?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1880903
date-type-field.png

Thank you for sharing the admin access.

During troubleshooting, I noticed that the "birth" and the "death" custom field values seem to be imported in the "m-d-Y" format.
( e.g. 12-19-2020 )

But the date type custom fields added through the Toolset Types plugin store the date/time values in the UNIX timestamp format.
( ref: hidden link )

If you'd like these dates to be properly processed by the View's date filters, the first step would be to convert them into UNIX timestamp. These are the steps that I'll recommend, after making a full back-up copy of the website.

1. Please go to WP Admin -> Toolset -> Custom Fields and add a new custom field group for the "Obituary" post type.
( ref: https://toolset.com/lesson-placement/lesson-placements-1620889-1620037/ )

In this new field group, you can add two "Date" type custom fields "Birth date" (slug: birth-date ) and "Death date" (slug: death-date ).
( example screenshot attached )

2. The next step is to fill these Toolset Types date fields with the values from the existing "birth" and the "death" custom fields, in each "Obituary" post.

In your active theme's "functions.php" file, you can add the following custom code:


function convert_to_timestamp_func($number)
{
	$d = DateTime::createFromFormat('m-d-Y H:i:s', $number.' 00:00:00');
	if ($d === false) {
		return "";
	} else {
		return $d->getTimestamp();
	}
}

add_shortcode( 'convert_dates_to_timestamp', 'convert_dates_to_timestamp_func');
function convert_dates_to_timestamp_func()
{
	$post_type = 'obituary';

	$old_birth_field = 'birth';
	$new_birth_field = 'birth-date';

	$old_death_field = 'death';
	$new_death_field = 'death-date';

	$args = array(
		'posts_per_page'   => -1,
		'post_type'        => $post_type,
		'post_status'      => 'publish'
	);

	$posts_array = get_posts( $args );

	foreach ( $posts_array as $post ) {
		$old_birth_value = get_post_meta( $post -> ID, $old_birth_field, true );
		if(!empty($old_birth_value)) {
			$new_birth_value = convert_to_timestamp_func($old_birth_value);
			if(!empty($new_birth_value)) {
				update_post_meta( $post -> ID, 'wpcf-'.$new_birth_field, $new_birth_value );
			}
		}

		$old_death_value = get_post_meta( $post -> ID, $old_death_field, true );
		if(!empty($old_death_value)) {
			$new_death_value = convert_to_timestamp_func($old_death_value);
			if(!empty($new_death_value)) {
				update_post_meta( $post -> ID, 'wpcf-'.$new_death_field, $new_death_value );
			}
		}
	}
}

This code will cycle through all the published "Obituary" posts, get the values of the existing "birth" and the "death" custom fields, convert them into UNIX timestamp and then save those values in the new date type custom fields, respectively.

3. To execute the code, you can create a new temporary page and add the following shortcode in it:


[convert_dates_to_timestamp]

Visit this page's front-end and the code will execute in the background. After that you can check few "Obituary" posts to confirm if the custom field values have been populated in the new date type fields.

Once confirmed, you can remove the page from step 3 and the custom code added in step 2.

After this transformation of custom fields values, you'll be able to use the date filters in the view's filters.

Note: For future "Obituary" posts, please use the Type's date type fields for storing the birth and the death dates.

In your content, you can show the output of these date fields in any date format, including the "m-d-Y" format:
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/#date )


[types field='birth-date' style='text' format='m-d-Y'][/types]
[types field='death-date' style='text' format='m-d-Y'][/types]

#1882741

My issue is resolved now. Thank you!