Passer la navigation

[Résolu] Split: View filter by distance – How to sort by distance

This support ticket is created Il y a 3 years, 9 months. 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 3 réponses, a 2 voix.

Dernière mise à jour par Minesh Il y a 3 years, 9 months.

Assisté par: Minesh.

Auteur
Publications
#2443981

How can I add a sort by distance to the view shortcode?

#2443997

Minesh
Supporter

Les langues: Anglais (English )

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

Hello. Thank you for contacting the Toolset support.

As a base from what distance you want to sort the results, is it from the parent location address as a base to sort it?

#2444001

Hi Minesh. That's correct.

So for this page the Auburn Listing should appear second as the distance is 1.1 from the current_page location.

lien caché

If possible I would also like to have 'Verified' items that will automatically sort to the top of the pile, and ignore distance.

#2444051

Minesh
Supporter

Les langues: Anglais (English )

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

To set the distance order by, I've set the ordering section to order by the lat-long field ASC with Distance from "fixed" source.
=> lien caché
Screenshot: lien caché

Then to set the fixed distance as your current posts lat-long value, I've added the following filter to "Custom Code" section offered by Toolset:
=> lien caché

add_filter( 'wpv_view_settings', 'func_set_distance_orderby_address', 10, 2 );
 function func_set_distance_orderby_address( $view_settings, $view_id ) {
    if ( $view_id == 88 ) { 
      	global $post;
      
        $view_settings['distance_order']['center'] = get_post_meta($post->ID,'wpcf-post-lat-long',true);
     
    }
    return $view_settings;
} 

More info:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

Can you please confirm it works as expected now.