How to sort store location by specific category order in WP store locations

i want show if store has specific category then should be show in first and then order by distance

add_filter( 'wpsl_sql', 'custom_sql' );

function custom_sql() {
     global $wpdb, $wpsl_settings,$wpsl;
           
    // Set a limit for the results and order them by distance.    
    if ( isset( $_GET['autoload'] )  $_GET['autoload'] ) {
        $limit = '';

        if ( $wpsl_settings['autoload_limit'] ) {
            $limit = 'LIMIT %d';
        }

        $sql_sort = 'ORDER BY distance ASC '. $limit;
        $sql_sort = 'ORDER BY field(ID, 9, 1, 4)'. $limit;
        
    } else {
        $sql_sort = 'HAVING distance  %d ORDER BY distance ASC LIMIT 0, %d';
        //$sql_sort = 'ORDER BY field( $wpdb-postmeta.meta_key, 9, 1, 4)'. $limit;
    }

    if ( isset( $args['autoload'] )  $args['autoload'] ) {
        $limit = '';

        if ( $wpsl_settings['autoload_limit'] ) {
            $limit = 'LIMIT %d';
            $placeholder_values[] = $wpsl_settings['autoload_limit'];
        }

        $sql_sort = 'ORDER BY distance ASC '. $limit;
    } else {
        //array_push( $placeholder_values, $this-check_store_filter( $args, 'search_radius' ), $this-check_store_filter( $args, 'max_results' ) );
        $sql_sort = 'HAVING distance  %d ORDER BY distance ASC LIMIT 0, %d';
        $sql_sort = 'ORDER BY lat ASC '. $limit;

    }
    if ( empty( $args ) ) {
        $args = $_GET;
    }

    if ( isset( $args['filter'] )  $args['filter'] ) {
        $filter_ids = array_map( 'absint', explode( ',', $args['filter'] ) );
        $cat_filter = INNER JOIN $wpdb-term_relationships AS term_rel ON posts.ID = term_rel.object_id
                       INNER JOIN $wpdb-term_taxonomy AS term_tax ON term_rel.term_taxonomy_id = term_tax.term_taxonomy_id
                              AND term_tax.taxonomy = 'wpsl_store_category'
                              AND term_tax.term_id IN ( . implode( ',', $filter_ids ) . );
    } else {
        $cat_filter = '';
    }
    
    if ( $wpsl-i18n-wpml_exists() ) {
        $group_by = 'GROUP BY lat';
    } else {
        $group_by = 'GROUP BY posts.ID';
    }
    // The default query that selects store location that fall within the selected radius. 
    $sql = SELECT post_lat.meta_value AS lat,
                   post_lng.meta_value AS lng,
                   posts.ID, 
                   ( %d * acos( cos( radians( %s ) ) * cos( radians( post_lat.meta_value ) ) * cos( radians( post_lng.meta_value ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( post_lat.meta_value ) ) ) ) 
                AS distance        
              FROM $wpdb-posts AS posts 
        INNER JOIN $wpdb-postmeta AS post_lat ON post_lat.post_id = posts.ID AND post_lat.meta_key = 'wpsl_lat'
        INNER JOIN $wpdb-postmeta AS post_lng ON post_lng.post_id = posts.ID AND post_lng.meta_key = 'wpsl_lng'
        $cat_filter
        WHERE posts.post_type = 'wpsl_stores'
                       AND posts.post_status = 'publish' $group_by $sql_sort;
             //WHERE posts.post_type = 'wpsl_stores' AND posts.post_status = 'publish' $sql_sort;    
    
    return $sql;
}

Topic order wp-query location-search sort Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.