Skip Navigation

[Resolved] 1 Date Filter for Different Views

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

Problem:
The user would like to filter two views with the same date filter.

Solution:
Each view needs to have its own filter. Custom code is required to hide, and synchronize the filter, of the second view from the first view.
Because the views are using AJAX the custom code needs to be run when the views update. Check an example here https://toolset.com/forums/topic/1-date-filter-for-different-views/#post-2239831

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/adding-custom-javascript-code-to-views-that-use-ajax/

This support ticket is created 3 years 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: Africa/Casablanca (GMT+01:00)

This topic contains 5 replies, has 2 voices.

Last updated by camila 3 years ago.

Assisted by: Jamal.

Author
Posts
#2238289

Hello,
We have a page with 2 different Views that each one has a date filter, is there a way via javascript or another way that if the user sets the first date filter the second one gets set as well?
thanks

#2238331

Hello and thank you for contacting the Toolset support.

Can you share a URL where we can see this? Do you want both views to search with same value when the search is performed in one of them?

I wonder if you must keep both filters on the page, or keeping just one would be enough?

Would you allow me temporary access to your website to check this further? Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#2239003

Well, keeping one date filter on the page may be easier than synchronizing multiple date filters. The trick is to have the date filter on one view, and have query filters based on the same URL parameters on the other views.

When you create a date filter, it will automatically create a query filter, however, the query filter will not be readable to know the actual conditions used on it. Check this screenshot hidden link
I can't tell what conditions do you use, I assume you want the posts with a date between the date filters, right?

So, I'll let you remove the date filters on the other views, and create their corresponding query filters based on the same URL parameters:
- wpv-wpcf-tax-sale-publication-date_min
- wpv-wpcf-tax-sale-publication-date_max

Does it make sense?

#2239151

Hi Jamal,
Can you please help us? Not sure how to do it. I tried to remove it and it did not let me.
The conditions are to get posts between the 2 publications date. Custom field: wpcf-tax-sale-publication-date
The 2 Views with Date Filter are:
1. Search - Newsletter
2. Search - Newsletter - Members Only.
so basically if I search on number 1, the number 2 view will get updated.
thanks

#2239831

Because both views are using AJAX, my previous suggestion is not relevant anymore. For this case, we'll need Javascript code. I made an example to explain the idea.

The idea is to put Javscript code on the second view and make it react to the changes on the first view. The code will:
- Hide the view's search form.
- Synchronize the date filters values.
- Trigger a form submit when the first view's form is submitted.
This is an example code:

function handleDatePickersSync57241 ($){
	// hide the views filters
	$('.js-wpv-filter-form-57241').hide();
	
	// sync datepickers
	// min date
	$('.js-wpv-filter-form-57231 .js-wpv-date-param-wpv-wpcf-tax-sale-publication-date_min-value').on('change', function(){
		$('.js-wpv-filter-form-57241 .js-wpv-date-param-wpv-wpcf-tax-sale-publication-date_min-value').val($(this).val()).trigger('change')
	})
	// max date
	$('.js-wpv-filter-form-57231 .js-wpv-date-param-wpv-wpcf-tax-sale-publication-date_max-value').on('change', function(){
		$('.js-wpv-filter-form-57241 .js-wpv-date-param-wpv-wpcf-tax-sale-publication-date_max-value').val($(this).val()).trigger('change')
	})
	
	// mirror submit form
	$('.js-wpv-filter-form-57231 .js-wpv-submit-trigger').on('click', function(){
		setTimeout(function(){ $('.js-wpv-filter-form-57241 .js-wpv-submit-trigger').click(); }, 200);
	})
}

jQuery(handleDatePickersSync57241);

jQuery( document ).on( 'js_event_wpv_parametric_search_form_updated', function( event, data ) {
	/**
	* data.view_unique_id (string) The View unique ID hash
	* data.view_changed_form (object) The jQuery object for the View form after being updated
	* data.view_changed_form_additional_forms_only (object) The jQuery object containing additional forms from other instances of the same View inserted using the [wpv-form-view] shortcode
	* data.view_changed_form_additional_forms_full (object) The jQuery object containing additional forms from other instances of the same View inserted using the [wpv-view] shortcode
	*/
	handleDatePickersSync57241(jQuery);
});

The second view's ID is 57241, If you want to add a 3rd view, copy the following code into it and replace all the IDs on it.

#2240077

My issue is resolved now. Thank you!