Navigation überspringen

[Gelöst] Split: View filter by distance – How to sort by distance

This support ticket is created vor 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 -

Zeitzone des Unterstützers: Asia/Kolkata (GMT+05:30)

Dieses Thema enthält 3 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Minesh vor 3 years, 9 months.

Assistiert von: Minesh.

Author
Artikel
#2443981

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

#2443997

Minesh
Unterstützer

Sprachen: Englisch (English )

Zeitzone: 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.

versteckter Link

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
Unterstützer

Sprachen: Englisch (English )

Zeitzone: 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.
=> versteckter Link
Screenshot: versteckter Link

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:
=> versteckter Link

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.