Passer la navigation

[Résolu] Ordering results by distance only when the distance filter is used

This support ticket is created Il y a 2 years. 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
- 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 -

Fuseau horaire du supporter : Asia/Kolkata (GMT+05:30)

Ce sujet contient 5 réponses, a 2 voix.

Dernière mise à jour par Minesh Il y a 1 year, 12 months.

Assisté par: Minesh.

Auteur
Publications
#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: lien caché

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

Les langues: Anglais (English )

Fuseau horaire: 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: lien caché
- 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

Les langues: Anglais (English )

Fuseau horaire: 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

Les langues: Anglais (English )

Fuseau horaire: 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

Les langues: Anglais (English )

Fuseau horaire: 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.