Skip Navigation

[Resolved] Reset filters without resetting results including pagination

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 19 replies, has 2 voices.

Last updated by Minesh 6 months, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2704690
Edit View ‹ The LockSmith Company — WordPress.png
2700004-image_9.png

Tell us what you are trying to do?
This site has a database of keys that are returned using a toolset view search and filters. The filters are dependant on each other.
The filters return results based on options already selected.
We are looking for a way to render results when a search is submitted but also reset all the filters.
We found a way to make this work in another ticket here:
https://toolset.com/forums/topic/reset-filters-without-resetting-results/#post-2701118

But we need it to also keep the results the same when clicking the next page button.

Is there any documentation that you are following?
https://toolset.com/course-lesson/displaying-search-results-on-a-different-page/
https://toolset.com/related-lesson/view/

Is there a similar example that we can see?
Not that I can find.

What is the link to your site? hidden link

Below is the JS we are currently using:

// Clear filter fields when searching from the homepage
jQuery('input[type="text"][name="wpv_post_search"]').val('');
jQuery('select[name="car_year"]').val('').trigger('change');
jQuery('select[name="car_make"]').val('').trigger('change');
jQuery('select[name="car_model"]').val('').trigger('change');

//Clear the filter fields after the search is updated from the search page
jQuery(document).ready( function(event, data) {
jQuery('input[type="text"][name="wpv-post-search"]').val('');
jQuery('select[name="car_model"]').val('').change();
jQuery('select[name="car_make"]').val('').change();
jQuery('select[name="car_year"]').val('').change();
});

How can we modify the view so it keeps the search results the same as we are clicking the next and previous page buttons?

#2704779

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

There is no such feature available.

Do you mean that when you click on reset button - it reloads the page and pagination but what you want is when you click on reset button you want to reset the filter controls to default state but keep result as it is. Is this correct? If no - please share your exact requirement with test case to follow.

If you can share problem URL and admin access details I will check if I can offer any workaround but please note that its custom code and there is no guarantee that solution will be provided.
- If you agree to this:

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2704912

Minesh,

We are not using or triggering the reset button. That button works as expected on the site. Clearing the filters also works as expected with the JS that's on the site. However, the next and previous buttons do not work as expected.

I am using JS to clear the select fields when we load a new page so that the filters are presented for a fresh search on each page as some of the filters are dependent on each other. When clicking the next button or previous button for pagination, it shows results as if no filters were applied as if it is performing a new search.

#2704997

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share admin access details and one or two use case with exact steps I will have to follow to see the issue.

Once I review that information and see the issue I will be able to guide you in the right direction and see if there is any possible solution available or not.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2706106

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for sharing admin access details and its working.

However - it seems you forget to share exact steps I will have to follow to see the issue. Can you please share couple of use case with exact steps I will have to follow to see the issue and also share what will be your expected result with that.

#2706825

Hi Minesh,

I made this video showing how the results render and explain exactly what we are trying to do:
hidden link

#2707355

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

This was really challenging task as view incorporates with the filters you reset the filter using the custom javascript code you added. The view filters are cleaned as you reset the filters to default options.

// Clear filter fields when searching from the homepage
jQuery('input[type="text"][name="wpv_post_search"]').val('');
jQuery('select[name="car_year"]').val('').trigger('change');
jQuery('select[name="car_make"]').val('').trigger('change');
jQuery('select[name="car_model"]').val('').trigger('change');


jQuery(document).on(' js_event_wpv_parametric_search_triggered', function(event,data){
  if(data.update_results == false){
    let formSelects = jQuery('.keys-search__filter form.wpv-filter-form select');
    formSelects.each(function(){
      jQuery(this).css({'pointer-events': 'none', 'opacity': '.4'});
      console.log('changed');
    })
  }
})


//Clear the filter fields after the search is updated from the search page
jQuery(document).ready( function(event, data) {
  jQuery('input[type="text"][name="wpv-post-search"]').val('');
  jQuery('select[name="car_model"]').val('').change();
  jQuery('select[name="car_make"]').val('').change();
  jQuery('select[name="car_year"]').val('').change();
});

==============================================
var urlParam = function(name){
  var url = window.location.href;
  if(url.includes(name)){
	var param = new RegExp('[\?&]' + name + '=([^&#]*)').exec(url),
        results = param[1];
  }else{
    var results = null;
  }
	return results;
}

function wst_add_key(key,vin,type) {
  var form_input = jQuery('#input_3_1'),
  	  current_value = form_input.val();
  if(urlParam('car_year')!== null){
    var year = urlParam('car_year');
  }else{
    var year = '';
  }
  if(urlParam('car_make')!== null){
    var make = urlParam('car_make');
  }else{
    var make = '';
  }
  if(urlParam('car_model') !== null){
    var model = urlParam('car_model');
  }else{
    var model = '';
  }
  //console.log('year param ' + year);
  //console.log('make param ' + make);
  //console.log('model param ' + model);

  if(current_value == 0){
    form_input.val(key + ' | ' + vin + ' | ' + year + ' ' + make + ' ' + model + ' | ' + type);
  }else{
    form_input.val(current_value + ', ' + key + ' | ' + vin + ' | ' + year + ' ' + make + ' ' + model + ' | ' + type);
  }
}
jQuery('.add_key_btn').on('click', function(){
  var btn_data = jQuery(this).data('key'),
      key_type = jQuery(this).data('key-type'),
      errorMsg = jQuery(this).siblings('.vin-error'),
      vin_input = jQuery(this).siblings('.vin-num-input'),
      vin_val = vin_input.val();
  if(!vin_val){
    errorMsg.addClass('active');
  }else if(vin_val){
  	wst_add_key(btn_data,vin_val,key_type);
    if(errorMsg.hasClass('active')){
      errorMsg.removeClass('active');
    }
    vin_input.val('');
    jQuery('.btn.wpv-reset-trigger').trigger('click');
  }
  
})
// After AJAX re init the event listener
jQuery( document ).on( 'js_event_wpv_pagination_completed', function( event, data ) {
    jQuery('.add_key_btn').on('click', function(){
    var btn_data = jQuery(this).data('key'),
        key_type = jQuery(this).data('key-type'),
      	errorMsg = jQuery(this).siblings('.vin-error'),
      	vin_input = jQuery(this).siblings('.vin-num-input'),
      	vin_val = vin_input.val();
  	if(!vin_val){
   	  errorMsg.addClass('active');
  	}else if(vin_val){
  	  wst_add_key(btn_data,vin_val,key_type);
      if(errorMsg.hasClass('active')){
        errorMsg.removeClass('active');
      }
      vin_input.val('');
      jQuery('.btn.wpv-reset-trigger').trigger('click');
  	}
  })
});
jQuery(document).on('js_event_wpv_parametric_search_results_updated', function (event, data) {
  let removeThis = jQuery('#lightbox .tb-lb-dataContainer'),
      moveClose = jQuery('#lightbox .tb-lb-dataContainer .lb-data .lb-closeContainer');
  if(moveClose.length > 0){
    jQuery('#lightbox .lb-dataContainer .lb-data').append(moveClose[0]);
  }
  removeThis.remove();
  jQuery('.add_key_btn').on('click', function(){
    var btn_data = jQuery(this).data('key'),
        key_type = jQuery(this).data('key-type'),
      	errorMsg = jQuery(this).siblings('.vin-error'),
      	vin_input = jQuery(this).siblings('.vin-num-input'),
      	vin_val = vin_input.val();
  	if(!vin_val){
   	  errorMsg.addClass('active');
  	}else if(vin_val){
  	  wst_add_key(btn_data,vin_val,key_type);
      if(errorMsg.hasClass('active')){
        errorMsg.removeClass('active');
      }
      vin_input.val('');
      //jQuery('.btn.wpv-reset-trigger').trigger('click');
  	}
  })
});

To make this works I've added the following filter hook "wpv_filter_query" to "Custom Code" section offered by Toolseat:

add_filter('wpv_filter_query', 'func_add_missing_url_params', 99, 3);
function func_add_missing_url_params($query_args, $setting,$view_id) {
    
    if($view_id == 7539) {
      
      if($query_args['paged']==1 and  isset($_GET['wpv_view_count'])) {
           
        	         
            if(isset($_GET['wpv_post_search']) and !empty($_GET['wpv_post_search'])){
                         
                    if(!isset($_COOKIE["wpv_post_search"])) {
                      setcookie("wpv_post_search", $_GET['wpv_post_search'], time() + (86400 * 30), "/");
                    }else{
                      $_COOKIE["wpv_post_search"]=$_GET['wpv_post_search'];
                    }
              
              }else if(isset($_GET['car_year']) and !empty($_GET['car_year'])){
              
                    if(!isset($_COOKIE["wpv_post_search"])) {
                      setcookie("car_year", $_GET['car_year'], time() + (86400 * 30), "/");
                    }else{
                      $_COOKIE["car_year"]=$_GET['car_year'];
                    }
              
            }else if(isset($_GET['car_make']) and !empty($_GET['car_make'])){
                        
                    if(!isset($_COOKIE["car_make"])) {
                      setcookie("car_make", $_GET['car_make'], time() + (86400 * 30), "/");
                    }else{
                      $_COOKIE["car_make"]=$_GET['car_make'];
                    }

            }else if(isset($_GET['car_model']) and !empty($_GET['car_model'])){
             
                    if(!isset($_COOKIE["car_model"])) {
                      setcookie("car_model", $_GET['car_model'], time() + (86400 * 30), "/");
                    }else{
                      $_COOKIE["car_model"]=$_GET['car_model'];
                    }
            }
         
        
      }else {
         
            if( ( 
              (isset($_COOKIE['wpv_post_search']) and !empty($_COOKIE['wpv_post_search']))
              or (isset($_COOKIE['car_year']) and !empty($_COOKIE['car_year']))
              or (isset($_COOKIE['car_make']) and !empty($_COOKIE['car_make']))
              or (isset($_COOKIE['car_model']) and !empty($_COOKIE['car_model']))
                ) and (isset($_GET['wpv_view_count']) and $query_args['paged']>1 ) ) {
     
     
       				$query_args['meta_query'] = array();
    
                    if(!empty($_COOKIE['wpv_post_search'])){
                                  $query_args['s'] =  $_COOKIE['wpv_post_search']; 
                    }else if(!empty($_COOKIE['car_year'])){

                                $query_args['meta_query'][] = array('key'=>'car_year',
                                                            'value'=> $_COOKIE['car_year'],
                                                            'type'=>'CHAR',
                                                            'compare'=>'=');

                    }else if(!empty($_COOKIE['car_make'])){
                      $query_args['meta_query'][] = array('key'=>'car_make',
                                                            'value'=>$_COOKIE['car_make'],
                                                            'type'=>'CHAR',
                                                            'compare'=>'=');


                    }else if(!empty($_COOKIE['car_model'])){
                      $query_args['meta_query'][] = array('key'=>'car_model',
                                                            'value'=>$_COOKIE['car_model'],
                                                            'type'=>'CHAR',
                                                            'compare'=>'=');

                    }      
    	}   
        
      }
       
      }
    return $query_args;
}

Please note that you will have to compromise with the pagination method. Above code will work only with option "Pagination enabled with manual transition and page reload" set under "Pagination and Slider" settings.

Can you please confirm it works as expected now:
- hidden link

#2707811
image.png

Hey Minesh,

Thank you for your work here.
It looks like what you did to edit the loop item in keys search is not rendering the second page but a list of parameters:
hidden link

Is there any way this can be fixed promptly?

#2707938

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sorry, at one place I forget to remove the debug code.

Can you please confirm it works as expected now:
- hidden link

I've adjusted the code added to "Custom Code" section as given under:
=> hidden link

add_filter('wpv_filter_query', 'func_add_missing_url_params', 99, 3);
function func_add_missing_url_params($query_args, $setting,$view_id) {
    
    if($view_id == 7539) {
      
      
      if(!isset($_GET['wpv_view_count'])) {
         if(isset($_COOKIE['car_year'])){
           unset($_COOKIE['car_year']);
         }else if(isset($_COOKIE["wpv_post_search"])){
            unset($_COOKIE['wpv_post_search']);
         }else if(isset($_COOKIE["car_make"])){
            unset($_COOKIE['car_make']);
         }else if(isset($_COOKIE["car_model"])){
            unset($_COOKIE['car_model']);
         }
         
      }
   //   if($query_args['paged']==1 and  isset($_GET['wpv_view_count'])) {
           
        	         
            if(isset($_GET['wpv_post_search']) and !empty($_GET['wpv_post_search'])){
              
                    if(!isset($_COOKIE["wpv_post_search"])) {
                      setcookie("wpv_post_search", $_GET['wpv_post_search'], time() + (86400 * 30), "/");
                    }else{
                      $_COOKIE["wpv_post_search"]=$_GET['wpv_post_search'];

                    }
              
             }else if(isset($_GET['car_year']) and !empty($_GET['car_year'])){
            
                    if(!isset($_COOKIE["wpv_post_search"])) {
                      setcookie("car_year", $_GET['car_year'], time() + (86400 * 30), "/");
                    }else{
                      $_COOKIE["car_year"]=$_GET['car_year'];
                    }
              
             }else if(isset($_GET['car_make']) and !empty($_GET['car_make'])){
                         
                    if(!isset($_COOKIE["car_make"])) {
                      setcookie("car_make", $_GET['car_make'], time() + (86400 * 30), "/");
                    }else{
                      $_COOKIE["car_make"]=$_GET['car_make'];

                    }

             }else if(isset($_GET['car_model']) and !empty($_GET['car_model'])){
            
                    if(!isset($_COOKIE["car_model"])) {
                      setcookie("car_model", $_GET['car_model'], time() + (86400 * 30), "/");
                    }else{
                      $_COOKIE["car_model"]=$_GET['car_model'];
                    }
             }
        
        
        
    //  }else {
      
 
     
            if( ( 
              (isset($_COOKIE['wpv_post_search']) and !empty($_COOKIE['wpv_post_search']))
              or (isset($_COOKIE['car_year']) and !empty($_COOKIE['car_year']))
              or (isset($_COOKIE['car_make']) and !empty($_COOKIE['car_make']))
              or (isset($_COOKIE['car_model']) and !empty($_COOKIE['car_model']))  
            ) and (isset($_GET['wpv_view_count'])  ) ) {
                 //(isset($_GET['wpv_view_count']) and $query_args['paged']>1 ) ) {
     
     
       				$query_args['meta_query'] = array();
      
    
                    if(!empty($_COOKIE['wpv_post_search'])){
                                  $_GET['wpv_post_search']=$_COOKIE["wpv_post_search"];
                                  $query_args['s'] =  $_COOKIE['wpv_post_search']; 
                                  
                    }else if(!empty($_COOKIE['car_year'])){
								$_GET['car_year']=$_COOKIE["car_year"];
                                $query_args['meta_query'][] = array('key'=>'car_year',
                                                            'value'=> $_COOKIE['car_year'],
                                                            'type'=>'CHAR',
                                                            'compare'=>'=');

                    }else if(!empty($_COOKIE['car_make'])){
                       $_GET['car_make']=$_COOKIE["car_make"];
                      $query_args['meta_query'][] = array('key'=>'car_make',
                                                            'value'=>$_COOKIE['car_make'],
                                                            'type'=>'CHAR',
                                                            'compare'=>'=');


                    }else if(!empty($_COOKIE['car_model'])){
                      $_GET['car_model']=$_COOKIE["car_model"];
                      $query_args['meta_query'][] = array('key'=>'car_model',
                                                            'value'=>$_COOKIE['car_model'],
                                                            'type'=>'CHAR',
                                                            'compare'=>'=');

                    }
       
    	}   
        
      }
       
    //  }
    return $query_args;
}
#2707970

Minesh,

I appreciate you working on a snippet for this.
Unfortunately it is not doing what our client requested with this added snippet.

Issue: The filters on the side need to reset to default when searching or paginating
Right now when an option is selected it stays selected even when i try to change it.
hidden link

To review:

We want the filter on the side to go back to default
But we want to return the filtered results and search through them when clicking the next page.

#2707973

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I'm not sure but can you please delete the browser cache and server cache if any and check.

Please check the following recording.
- hidden link

I can see that the year field is back to default option after filtering the results.

Am I missing anything here.

#2708208

Sorry,

In order to test this you will need to make the snippet active again.
I disabled it and marked it inactive because I didn't want the client to encounter the error.
hidden link

#2708287

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I would like to confirm one thing here:
- hidden link

Is above page publicly available to all users or to only loggedin users?

#2708302

Its only visible to logged in users.

#2708308

Hello,

I didn't think this would take as long as it has, I had to deactivate that script again because its causing errors on the search and out client needs to use the search tool.
I have already spoken to our system administrators and we are creating a stage for this testing.
I will share the stage URL and login details here when its up.