// Sort results by distance

The following code will sort results by distance whenever a proximity facet is active (unless the Sort by dropdown is already in use):

<?php

// Add to your (child) theme's functions.php, excluding the above "<?php" line

add_action( 'pre_get_posts', function( $query ) {
    if ( ! class_exists( 'FacetWP_Helper' ) ) {
        return;
    }

    $facets_in_use = FWP()->facet->facets;
    $prefix = FWP()->helper->get_setting( 'prefix' );
    $using_sort = isset( FWP()->facet->http_params['get'][ $prefix . 'sort' ] );

    $is_main_query = false;
    if ( is_array( FWP()->facet->template ) ) {
        if ( 'wp' != FWP()->facet->template['name'] || true === $query->get( 'facetwp' ) ) {
            $is_main_query = true;
        }
    }

    if ( ! empty( $facets_in_use ) && ! $using_sort && $is_main_query ) {
        foreach ( $facets_in_use as $f ) {
            if ( 'proximity' == $f['type'] && ! empty( $f['selected_values'] ) ) {
                $query->set( 'orderby', 'post__in' );
                $query->set( 'order', 'ASC' );
            }
        }
    }
});