Skip Navigation

[Resolved] Order by Distance Not Working Using Legacy View And Address Fields

This support ticket is created 2 years, 2 months ago. 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 30 replies, has 3 voices.

Last updated by Waqar 2 years ago.

Assisted by: Waqar.

Author
Posts
#2303405

Can I send this URL?

#2304703

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I've set your next reply as private so that you can share the link for the website's duplicator package.

#2305567

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

The zip file that you've shared is not complete. There is a duplicator package available on the website, but that only covers 'database' and not the files.

Do I have your permission to create a new complete duplicator package and download it directly from the website?
(depending on the size I may also have to install the Duplicator Pro plugin for this)

#2305927

Yeah Sorry about that. You can do whatever.

I suggest not including uploads in your duplication as there’s a few gigabytes of images.

Let me know of you need any help.

#2306795

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for the permission.

I tried to create a duplicator package using the Duplicator Pro plugin, but even with the 'uploads' excluded, it fails every time.

Most likely, your hosting server doesn't have enough free disk space available and I'm also seeing similar errors and warnings in the browser's console. It is not even allowing to delete the Duplicator Pro plugin or install another alternate plugin 'Cloudways WordPress Migrator'.

Can you please get in touch with your hosting support and sort out this disk space issue?

#2333483

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for the update and I've managed to download the latest duplicator package from your website.

I'll need to run some tests on this clone and will share the findings, as soon as this testing completes.

Thank you for your patience.

#2334367

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting.

During troubleshooting, I found this custom function in the active theme's "functions.php" file, which is responsible for ordering the results of the view "Cities - Near INPUT City - List", based on the location passed in the shortcode attribute "fromcenter":


add_filter( 'wpv_view_settings', 'ts_sort_map_by_distance_from_shortcode', 99, 2 );

function ts_sort_map_by_distance_from_shortcode( $view_settings, $view_id ) {
  global $WP_Views;
  $views = array( 120058 );
  if ( in_array( $view_id, $views) ) {
    $shortcode_atts = isset( $WP_Views->view_shortcode_attributes[0] ) ? $WP_Views->view_shortcode_attributes[0] : null;
    if ( isset($shortcode_atts['fromcenter']) ) {
      $query_center = $shortcode_atts['fromcenter'];
      $view_settings['distance_order'] = array(
        "source" => "fixed",
        "center" => $query_center
      );
    }
  }
  return $view_settings;
}

The code works, when the view is used directly, but not when it is placed inside an archive. To make it work for both cases, you can use the slightly updated function:


add_filter( 'wpv_view_settings', 'ts_sort_map_by_distance_from_shortcode', 99, 2 );

function ts_sort_map_by_distance_from_shortcode( $view_settings, $view_id ) {
  global $WP_Views;
  $views = array( 120058 );
  if ( in_array( $view_id, $views) ) {

    if(isset($WP_Views->view_shortcode_attributes[0]['fromcenter'])) {
      $query_center = $WP_Views->view_shortcode_attributes[0]['fromcenter'];
    }
    elseif(isset($WP_Views->view_shortcode_attributes[2]['fromcenter'])) {
      $query_center = $WP_Views->view_shortcode_attributes[2]['fromcenter'];
    }

    if ( isset($query_center) ) {
      $view_settings['distance_order'] = array(
        "source" => "fixed",
        "center" => $query_center
      );
    }
  
  }

  return $view_settings;
}

After this change, the distance by ordering works on the taxonomy archive page too, in my tests on your website's clone.

#2334669

The distance sorting also does not work on another view featured here:
hidden link

which is on the wp archive 'City v2', using views shortcode cities-list-nearby

It displays custom post type centers, which all have addresses, and is basically 'from center' the same way but is not listing by distance, though the distance display shortcode works 🙂

#2334681

Can I duplicate that function and change the 120058 to a different view id and have it work (it try to) as long as I format the view short code the same?

#2334861

Also the sort by distance is still not working even with that updated code in functions, you can log in to the active site and see if there's anything I've done wrong, it's all backed up and cached anyways, but post id 36831 is the archive template that displays the city pages, and the view ID is 120058 as you already know, as you can see it only is working with the filter within 80 miles of input from fromcenter, and is not sorting by distance, though the miles display

not solved on me end 🙁

#2336355

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

To extend the custom code snippet for other views, you can simply include their IDs, in a comma-separated format, in line# 5.

For example:


$views = array( 120058, 111111, 222222 );

Please replace 111111 and 222222 with your actual target views IDs and add more as needed.

On the live website, in the query filter settings for the view "Cities - Near INPUT City - List" ( ID: 120058 ), the "mapcenter" parameter was set, which I've changed to "fromcenter", as that is what I had on your website's clone and the custom code snippet also uses that.
( screenshot: hidden link )

The code snippet is sorting the results correctly on the clone that I have, but it is strange that it is acting differently on your live website.

Screenshot from clone for the archive at /in/wisconsin/appleton/:
hidden link

On the clone, I see a "no items found message" on the archive /in/wisconsin/altoona-wisconsin/ probably because you've added some listings after the duplicator package was created.

There are a few improvements planned in the upcoming version of the Toolset Maps plugin, which deals with distance-based ordering. You'll find a file named "toolset-maps-views-distance.php" in a zip file available, from that update:
hidden link

You can download it and replace it with a file of the same name in the Toolset Maps plugin folder:
toolset-maps/includes/

Let me know how it goes and make sure to clear all involved caches.

#2336475

I’ll try with the examples and screenshots you provided first, then the plug-in “beta” second. Thanks for your help so far! As you can tell by the site, distance based sorting works be greatly beneficial!

#2336501

To call the view from the archive page, I am using this:

[wpv-view name='cities-near-input-city-list' fromcenter='[types termmeta='si-addy-city-state'][/types]' parentstate='[parent_term_info output='name']' limit='12']

Could the issue be from how I'm calling it?

#2339399

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Please take your time and let me know how it goes.

Also, I couldn't find the shortcode from your last message. Can you please share exactly where it is being used?

#2340615

The issue is the address fields from the term data are not 'correct' in the query, but they /are/ correct in the query when its custom post, so what I did is wrap the city by distance view on term pages in a 'custom post type' view that takes the address from a custom post type within the current taxonomy, then display the distance view works correctly

this isn't really the most effective way at all, but it does work

I tried the new maps hotfix file, I see that you've made some smart changes, but that file alone doesn't actually work correctly as there are new variables being called, I can't remember off hand but its like tmaps_cache versus toolset_mapscache or something, which aren't set

I think the maps cache is broken for some of the term data, and that's another issue, because the distance filter works on 'some' cities, if I created a new city taxonomy, manually entered the city state in the google maps field, it would work, but none of the ones which have cached maps data worked.. super strange, not going to clear the cache tho as theres 40k centers with maps cache, 10k city pages with map cache, and then tons of calls within there that are all cached.. I don't think I could afford it if we cleared all that out

thanks for your help tho it did get us to the right solution, can't wait for new maps features, will def try again on a dev site with some other issues possibly resolved

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.