Skip Navigation

[Resolved] Ordering results by distance only when the distance filter is used

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

Last updated by Minesh 7 months, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2701206

I am trying to implement the solution provided to my previous support call https://toolset.com/errata/cannot-order-results-by-distance-in-views-created-with-the-block-editor/ but the first step in the instructions is:

"Set up the View with location search and order by the address field, without specifying as a distance (the missing option)."

However I only want to order the listing by the address field IF the user is using the distance filter. Is there a way to do that?

This is the search/directory I am working on: hidden link

I want the directory entries to be listed as they are (by post title) unless they are the results of a distance search.

Thank you!

#2701228

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Have you setup the view in legacy mode?
- https://toolset.com/course-lesson/enabling-legacy-version-of-toolset-views/

If yes:

I see on the page: hidden link
- The results are already order by post title ASC

How do you added the distance filter. Have you set the order by distance address field from the "Ordering" section?

Can you please share admin access details so I can check your view's configurations.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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.

#2701243

Minesh
Supporter

Languages: English (English )

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

Actually - it should work like that but I need to check your view's configuration.

I try to login with the user admin access details you shared but its not working at this end.

Can you please send me working admin access details.

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

#2701256

Minesh
Supporter

Languages: English (English )

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

Can you please check now.

I've added the following code to "Custom Code" section with code snippet "toolset-custom-code":

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'])) {

                       $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;
}

Can you please confirm it works as expected now.

#2701259

Hi Minesh

It now works as we wanted it to when you use the distance filter but if a user doesn't use the distance filter, e.g. just filters by IFS Training Level, the results are no longer ordered by post title.

I assume that's because even if the user doesn't fill in the distance search, when you submit the search it is still one of the filters.

I think though I can offer the client this choice.

Thanks
Lucy

#2701339

Minesh
Supporter

Languages: English (English )

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

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

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;
}

Can you please confirm it works as expected.

#2701916

That's great thank you. It's now working as we would like.