Skip Navigation

[Resolved] Address field access via PHP

This support ticket is created 5 years 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 5 years ago.

Assisted by: Christian Cox.

Author
Posts
#1483551

Tell us what you are trying to do?
Every post has a Toolset address field attached.
We also have a Locations taxonomy setup. Against every taxonomy term we have an Address field setup as well as a distance_radius numeric field.

In a PHP function we want to be able to determine the closest Locations taxonomy term based on the address of the post and the addresses stored against each Location taxonomy.

Another requirement is that if we are look at a Locations archive page, retrieve it's address and distance_radius and then list all posts who's address falls withing the radius (miles) of the Location taxonomy term address.

Is there any documentation that you are following?
Custom Fields and Address

Is there a similar example that we can see?
No

What is the link to your site?
hidden link

#1483607
Screen Shot 2020-01-28 at 3.27.05 PM.png

In a PHP function we want to be able to determine the closest Locations taxonomy term based on the address of the post and the addresses stored against each Location taxonomy.
Distance filters in Views are limited to Views that show posts, unfortunately, not terms or Users. So I don't have an easy way to calculate those distances or filter by those distances using Views and the Views PHP API. You would need custom PHP to calculate distance between the post address field and the term address fields, then compare all those distances. I can show you how to access custom field values but the actual geometric calculations and comparisons fall outside the scope of support we provide here since they are not directly related to Toolset and are fairly complex. Here is an example of such a calculation using lat/long values: https://www.geodatasource.com/developers/php

You could use the types_render_field function to get the lat and long of each address field without querying the DB directly. Documentation for this API is available here: https://toolset.com/documentation/customizing-sites-using-php/functions/#address

Example showing how to get the latitude of a post address field for post ID 12345:

$latitude = types_render_field( 'post-address-slug', array( 'format' => 'FIELD_LATITUDE', 'item' => '12345' ) ));

Example showing how to get the latitude of a term address field for term ID 5678:

$latitude = types_render_termmeta( 'term-address-slug', array( 'format' => 'FIELD_LATITUDE', 'term_id' => '5678' ) ));

Toolset also caches the lat/long of each geocoded address in the wp_toolset_maps_address_cache table of the database, so you could query the DB directly to access those values using the addresses stored in wp_postmeta, wp_usermeta, or wp_termmeta. Address fields from Types use the wpcf- prefix in the database, so if the slug for a term field in wp-admin is "term-location" then the termmeta key will be "wpcf-term-location".

Another requirement is that if we are look at a Locations archive page, retrieve it's address and distance_radius and then list all posts who's address falls withing the radius (miles) of the Location taxonomy term address.
This requirement is currently possible in Views without the need for custom code. Create a View of posts with a distance filter, where the distance center is set by a shortcode attribute, then use the Types field shortcode on the archive page to include the term address field in the View's shortcode attribute. Place the View shortcode in your WordPress Archive for this taxonomy, and include the term field shortcode attribute. The View shortcode would look something like this:

[wpv-view name="Name of your post View with distance filter" mapcenter="[types termmeta='term-address-field-slug' output='raw'][/types]"]

Example distance Query Filter screenshot attached.

#1483747

Thanks for the quick response. I understand that the calculations are complex which I didn't realise until I looked into it.

With regards to my first requirement... As Toolset does have distance calculations already included, is there a way to use these in some way. Or are these buried away and not easily callable?

With regards your answer to my second requirement and the use of the View shortcode, there is no way to add the distance_radius field. The only way the distance can be entered (as far as I can see) is having it fixed into the view itself, as your screenshot (and my testing) shows. Can't the distance be a parameter for the views shortcode?

#1485551

With regards to my first requirement... As Toolset does have distance calculations already included, is there a way to use these in some way. Or are these buried away and not easily callable?
We don't have a public PHP API for these equations, but it turns out we do have shortcode-based distance calculation available that can accommodate term fields as well as post fields:
https://toolset.com/documentation/user-guides/maps/display-on-google-maps/filtering-and-ordering-map-markers-by-distance/
The distance shortcode documentation is available here:
https://toolset.com/documentation/user-guides/maps/maps-shortcodes/#toolset-maps-distance-value
I was under the impression this shortcode was limited to calculations using post fields (like the distance filter in Views is limited to Views showing posts), but after doing some additional research I am happy to report I was mistaken. This means you could use the GUI in any wp-admin Visual Editor to generate the appropriate shortcode(s), and use do_shortcode() in PHP to execute those shortcodes in lieu of a true PHP API. Example shortcode showing how to calculate the distance from a pair of lat/long coordinates to an address field in some taxonomy term with the term ID 46:

[toolset-maps-distance-value target_source='termmeta' termmeta='wpcf-address-term-field-slug' termmeta_id='46' unit='mi' location='{30,100}']

...there is no way to add the distance_radius field. The only way the distance can be entered (as far as I can see) is having it fixed into the view itself...
From wp-admin, yes, however the Views PHP API can be used to programmatically set this value. Here's an example that manipulates the distance and center source:

add_filter( 'wpv_view_settings', 'tssupp_123_distance_radius', 99, 2 );
function tssupp_123_distance_radius ( $view_settings, $view_id ) {
  $views = array( 123 );
  if( in_array( $view_id, $views) ) {
    $view_settings['map_distance_filter']['map_distance'] = '1000';
    $view_settings['map_distance_filter']['map_center_source'] = 'shortcode_attr';
  }

  return $view_settings;
}

You would change 123 in 3 places to match the map View's ID, and change 1000 to be the desired radius.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_view_settings

Then your map View shortcode would look like this to display only the results (no filters) within 1000 units instead of whatever you set up in the View editor:

[wpv-view name="your-map-view-slug" view_display="layout" mapcenter="Pittsburgh, PA, USA"]