Skip Navigation

[Resolved] General text search and distance filter don't work together

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 4 replies, has 2 voices.

Last updated by Minesh 6 months, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2703055

I have a search facility that allows uses to do a general text search as well as setting various filters.

The text search works in combination with most filters but not the distance filter.

For example

- Text search 'adhd' and set IFS Training Level to IFS Level 3 gives the correct result.

- Text search 'adhd' and set Distance to within 40 miles of Cambridge, UK, does not apply the distance filter.

Thank you!

#2703158

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share problem URL where I can see the issue as well as admin access details.

*** 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.

#2703214

Minesh
Supporter

Languages: English (English )

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

Can you please share problem URL?

#2703221

hidden link

#2703506

Minesh
Supporter

Languages: English (English )

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

When I run a test with:
- Text search 'adhd' and set IFS Training Level to IFS Level 3 gives the correct result.
I see only the following post as result: Eleanor Carn

When I checked the post in backend the address field has the following value: "B30 2SU GB"

when I checked the address is not full address "B30 2SU GB". Can you please first setup correct full address. For testing purpose - I tried to check with "Lewis Road, Stirchley, Birmingham B30 2SU, UK" but not luck.

I see you setup text search with option "Title, body and custom fields" using relevanssi:
- https://toolset.com/documentation/user-guides/views/searching-texts-custom-fields-views-relevanssi/#integration-with-custom-searches

If you check the above Doc - it clearly says - just above "Known issues" heading:

Once you add a search filter that includes custom fields, the entire search operation is performed by the Relevanssi plugin, which means that Relevanssi is also responsible for providing the results order. This is why the Views sorting order options are not used. This includes ordering results by distance in a search for posts that include a Toolset Maps address field, where Relevanssi determines the ordering.

Here is the related ticket:
- https://toolset.com/forums/topic/combined-search-field-and-distance-filter-in-view/

However - I'm trying to provide you the best solution I can that might help you and if that does not suits your needs then you will have to make a decision to not combine both text search and distance search.

To offer you the solution, I've created the following view that will return the result without text search but distance and all other filters:
- hidden link
Where - added all the query filters to filter by URL param and just excluded the text search for this view:

Distance
Show posts within 40mi radius of address/coordinates provided using toolset_maps_distance_center URL parameter.

Taxonomy filter

Select posts with taxonomy:
Sessions slug in one of those set by the URL parameter wpv-session
eg.
hidden link

AND

Availability slug in one of those set by the URL parameter wpv-availability
eg. hidden link

AND

IFS Training Levels slug in one of those set by the URL parameter wpv-ifs-training-level
eg. hidden link

AND

IFS Qualifications slug in one of those set by the URL parameter wpv-ifs-qualification
eg. hidden link

Custom field filter
Select items with field:
Languages spoken is a string like URL_PARAM(wpv-wpcf-languages-spoken)

And configured the Loop Editor as given under - that will return the post IDs:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-item index=other][wpv-post-id],[wpv-item index=last][wpv-post-id]</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]0[/wpv-no-items-found][wpv-layout-end]

Then to the "Custom Code" section I've adjusted the code as given under:

function func_update_distance_radius_from_urlparam( $view_settings, $view_id ) {

  $allowed_views = array(2096,2478);

  if (in_array($view_id,$allowed_views)) {
    if(isset($_REQUEST['toolset_maps_distance_radius']) and
       $_REQUEST['toolset_maps_distance_radius']!='') {
      
       $view_settings['map_distance_filter']['map_distance']= $_REQUEST['toolset_maps_distance_radius'];
    }
    
   
  }
  return $view_settings;
}
add_filter( 'wpv_view_settings', 'func_update_distance_radius_from_urlparam', 5, 2 );


add_filter( 'wpv_view_settings', 'func_set_orderby_distance', 101, 2 );
function func_set_orderby_distance( $view_settings, $view_id ){

if ( $view_id == 1432 ) {

                    $address_field_slug = 'address'; 
                    
                    if(isset($_GET['toolset_maps_distance_center']) and $_GET['toolset_maps_distance_center'] != '' ) {

                       $view_settings['orderby'] = "field-wpcf-$address_field_slug";
                       $view_settings['orderby_as'] = 'DISTANCE';
                       $view_settings['order'] = 'ASC';
                       $view_settings['distance_order'] = array(
                                                                                      'source' => 'url_parameter',
                                                                                       'center' => '',
                                                                                       'url_parameter' => 'toolset_maps_distance_center'
                                                                                       );
                      }

}

return $view_settings;
}



add_filter( 'wpv_filter_query', 'func_text_search_with_distance', 101, 3 );
function func_text_search_with_distance($query_args,$view_settings, $view_id ){

if ( $view_id == 1432 ) {

                if( isset($_GET['wpv_post_search']) and !empty($_GET['wpv_post_search'])  and 
                    isset($_GET['toolset_maps_distance_center']) and !empty($_GET['toolset_maps_distance_center']) 
                    ){


			     $tax_query = $query_args['tax_query'];
			     $meta_query = $query_args['meta_query'];
							
                            $search_keyword = $_GET['wpv_post_search'];

                              //// searching results with relevanssi
                              $args  = array(
                                   's'           => $search_keyword,
                                   'numberposts' => -1,
                                   'fields'=> 'ids',
                                   'post_types'  => array( 'practitioner' ),
                                    'relevanssi'  => true,
									'tax_query' => $tax_query,
									'meta_query' => $meta_query
   
                                );

                               $text_search_result = new WP_Query( $args );
                                $text_search = array();
                                if(!empty($text_search_result->posts)){
                                    $text_search = $text_search_result->posts;
                                }
					
			    
                              //// getting distance search results
                              $geo_result = array();
                               $result = do_shortcode('[wpv-view name="get-all-distance-filter-results" cached="off"]');
                              $geo_result = explode(",",trim($result));

                            $final_result = array_intersect($text_search, $geo_result);
 
                             $query_args['post__in'] = $final_result ;  
 
      }

                       
   }
 return $query_args;
}

Can you confirm it works as expected now.

#2703681

Thank you once again for your help.

I will say to the client that if there are any further problems, we will need to separate the text search facility from the other filters, but it looks to me like it is working correctly now.

Thanks and best wishes
Lucy