Title: Multisort Order
Last modified: September 22, 2020

---

# Multisort Order

 *  [Vincenzo Casu](https://wordpress.org/support/users/vincent06/)
 * (@vincent06)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/multisort-order/)
 * Hi,
    I took a look at the page [https://wpstorelocator.co/document/change-search-results-order/](https://wpstorelocator.co/document/change-search-results-order/)
 * and I would like to know if I can do so to insert in the lateral template the
   country as a title and immediately under all the shops of that country ordered
   alphabetically…
 * in my custom wpsl template i have this code:
 *     ```
       ....
       $output .= "\t" . '<div id="wpsl-result-list">' . "\r\n";
       $output .= "\t\t" . '<div id="wpsl-stores" '. $autoload_class .'>' . "\r\n";
       $output .= "\t\t\t" . '<ul></ul>' . "\r\n";
       $output .= "\t\t" . '</div>' . "\r\n";
       $output .= "\t\t" . '<div id="wpsl-direction-details">' . "\r\n";
       $output .= "\t\t\t" . '<ul></ul>' . "\r\n";
       $output .= "\t\t" . '</div>' . "\r\n";
       $output .= "\t" . '</div>' . "\r\n";
       ....
       ```
   
 * wpsl-stores list is dynamically filled by the ajax request, how can I control
   this flow to enter the country names and sort the stores in order of country /
   alaphabetic?

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [Tijmen Smit](https://wordpress.org/support/users/tijmensmit/)
 * (@tijmensmit)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/multisort-order/#post-13442008)
 * If you want to change the search results template itself, then you need to use
   [this filter](https://wpstorelocator.co/document/wpsl_listing_template/).
 * Sorting the results by country is explained [here](https://wpstorelocator.co/document/change-search-results-order/).
 * So adding this code to the functions.php inside your active theme folder should
   make that work. Do make sure to flush the WPSL transient cache on the settings
   page ( tools section ).
 *     ```
       add_filter( 'wpsl_store_data', 'custom_result_sort' );
   
       function custom_result_sort( $store_meta ) {
   
           $custom_sort = array();
   
           foreach ( $store_meta as $key => $row ) {
               $custom_sort[$key] = $row['country'];
           }
   
           array_multisort( $custom_sort, SORT_ASC, SORT_REGULAR, $store_meta );
   
           return $store_meta;
       }
       ```
   
 *  Thread Starter [Vincenzo Casu](https://wordpress.org/support/users/vincent06/)
 * (@vincent06)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/multisort-order/#post-13443408)
 * Hello, thank you for your reply!
 * Is there any way to better group the results?
 * Let me explain: if I look for Spain, and for some reason there is a shop that
   is geographically closer, in the results this is shown first … instead if I look
   for Spain it is because I want Spain, then afterwards you can also show me the
   other shops … Your snippet groups by country, but it shows first France and then
   Spain even though I searched for Spain … I don’t know if I made it clear …
 * With this modification below it sorts the results first by distance and then 
   by country, but still not what I’m looking for …
 *     ```
       function wpsl_custom_result_sort($store_meta)
       {
   
          $custom_sort_1 = array();
          $custom_sort_2 = array();
          foreach ($store_meta as $key => $row)
          {
             $custom_sort_1[$key] = $row['distance'];
             $custom_sort_2[$key] = $row['country'];
          }
   
          array_multisort($custom_sort_1, SORT_ASC, SORT_REGULAR, $custom_sort_2, SORT_ASC, SORT_REGULAR, $store_meta);
   
          return $store_meta;
       }
       add_filter('wpsl_store_data', 'wpsl_custom_result_sort');
       ```
   
 * In fact, if there are stores in other countries that are closer than the position
   sought, always show those first …
 * do you have any suggestions?
    -  This reply was modified 5 years, 8 months ago by [Vincenzo Casu](https://wordpress.org/support/users/vincent06/).
    -  This reply was modified 5 years, 8 months ago by [Vincenzo Casu](https://wordpress.org/support/users/vincent06/).
 *  Thread Starter [Vincenzo Casu](https://wordpress.org/support/users/vincent06/)
 * (@vincent06)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/multisort-order/#post-13443500)
 * If you navigate to my dev site: [https://schenker.kolorweb.it/dealers/](https://schenker.kolorweb.it/dealers/)
 * As you can see the results are sorted first by distance from the starting point(
   which in this case is USA) and then by country, even if in the end I can’t find
   myself because the order always follows the distance first … a view like I did
   here ([https://schenker.kolorweb.it/all-dealers-list](https://schenker.kolorweb.it/all-dealers-list))
   taking the values manually with an ad hoc query …
 * I would like to have country and country distributor in order of country …
 * If a search is done, I would like to have the country searched (if it is in first
   position regardless of distance) and other results close to follow, always grouped
   by country …
 *  Plugin Author [Tijmen Smit](https://wordpress.org/support/users/tijmensmit/)
 * (@tijmensmit)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/multisort-order/#post-13452521)
 * ‘If a search is done, I would like to have the country searched (if it is in 
   first position regardless of distance) and other results close to follow, always
   grouped by country …’
 * This is not something that can be easily done, since you will first to [reverse geocode](https://developers.google.com/maps/documentation/geocoding/overview#reverse-example)
   the coordinates again with an API request. Nothing in the submitted data contains
   the country name.
 * I didn’t test it, but the $_GET data likely still contains the coordinates with
   [this filter](https://wpstorelocator.co/document/wpsl_store_data/). There you
   can reverse geocode the coordinates and find the searched country.
 * Then loop over the results in the wpsl_store_data filter and take out the locations
   that belong to that country in a separate array, and place the other locations
   in a different array by country and merge them in the end with the one with the
   searched country first.
    -  This reply was modified 5 years, 8 months ago by [Tijmen Smit](https://wordpress.org/support/users/tijmensmit/).

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Multisort Order’ is closed to new replies.

 * ![](https://ps.w.org/wp-store-locator/assets/icon-256x256.jpg?rev=1007784)
 * [WP Store Locator](https://wordpress.org/plugins/wp-store-locator/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-store-locator/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-store-locator/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-store-locator/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-store-locator/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-store-locator/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [Tijmen Smit](https://wordpress.org/support/users/tijmensmit/)
 * Last activity: [5 years, 8 months ago](https://wordpress.org/support/topic/multisort-order/#post-13452521)
 * Status: not resolved