Skip Navigation

[Resolved] Address field is not working as expected

This thread is resolved. Here is a description of the problem and solution.

Problem: I have postal code stored as a custom field on several posts. I would like to use that postal code in the address field for distance calculations, but the distance filter isn't working correctly using this imported data.

Solution: Postal code is usually too ambiguous for a distance filter, since similar postal codes can exist in multiple countries, etc. It's best to use an address selected from the autocomplete suggestions, since it is guaranteed to be mappable.

This support ticket is created 5 years, 4 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
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 14 replies, has 2 voices.

Last updated by Wim 5 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1359811

Wim

I have a custom search query based on a radius search. There is an adress field that has a postal code value in it. This postal code is being copied from another field when the post is saved.
However sometimes the postal code is copied as is, example given: 1234 AA . If this is the case the post can be found in the search on the frontend.
But sometimes when the postal code is saved in the adress field it has extra data in it, example given: 1234 AA [city], [Country]
If these values are added in the adress field the post will NOT show up while searching for the specific post.

Any idea why this is happening and is possible is there a way to prevent this from happening?

Note: the source field only contains the postal code, not the city and or country!

#1359847

There is an adress field that has a postal code value in it. This postal code is being copied from another field when the post is saved.
How exactly is the field value copied from another field? Please provide any custom code you have used for me to review. I'm not sure why the city and country would be added automatically like this. Manipulating address field values programmatically is generally not a good idea since the new address isn't validated ahead of time, but maybe I can spot something unusual in the code.

#1359849

Wim

Hi Christian,

Thanks for the quick reply!

this is the code I am using:

add_action( 'save_post', 'copy_location_to_multiline_field_admin', 100, 3 );
function copy_location_to_multiline_field_admin( $post_id, $post, $update ) {
  if ( $post->post_status == 'publish' && $post->post_type == 'post' ) {
    $location = get_post_meta( $post_id, 'postalCode', true);
    update_post_meta( $post_id, 'wpcf-postcode', $location );
  }
}
#1359863

Okay thanks. Can you provide examples of one or two addresses that now have the city and country added in the wpcf-postcode address field, even though the corresponding postalCode field does not contain that information? I will run some tests locally and see if I can replicate the problem.

#1359871

Wim

the postal code 3888 LL is from the source field
the value in the custom field data: wpcf-postcode is also 3888 LL
the value after the geocoding 3888 LL Uddel, Nederland in the toolset adress field

another example is 3846 AX

an example of a postal code that works ok: 3845 CA

For some etra information: these are all dutch postal codes.....

Thanks again for looking into this!
If you need any more information I will be happy to provide you with it.

#1359901

Wim

Just wondering: If its not possible to show / transfer only the postal code, is it the possible to make the radial search work with fields that have postal code only and fields with postal code + city + country? That would be a fix also I think.

#1359903

is it the possible to make the radial search work with fields that have postal code only
You must use geocoded locations in a Toolset address field to implement accurate distance-based searches. Postal code only usually isn't sufficient.

May I log in and see how this View is set up? Please let me know how I can find the View on the front-end of the site.

#1359923

Wim

By the way the posts where only the postal code is being used are accurate enough. It should not be too accurate, just roughly is more then enough 🙂
But the issue remains that only posts with just the postal code show up in the search and the posts with more information just do not despite the more accurate geolocation

#1360005
3845-ca.png

AS you can see in post 905, this address "3845 CA" has not been geolocated. On my local machine, the map is pointing to California, not anywhere in the Netherlands. Where do you expect the marker to be shown?

#1360015

Wim

Ah I see....it should be here 3845 CA Harderwijk.....

Is this the reason why the post is not showing? Because some do, some dont. On the frontend on the greenmonkeys.nl/vacatures only 18 posts are showing instead of 62 if you enter no search criteria

#1360035

Yes, I suspect this is at least part of the reason why the distance-based searches are not performing as you would expect. Without geocoding, the distance filter cannot complete its calculations...the addresses are too ambiguous.

#1360043

Wim

Allright, so if i update all posts manually with complete geocoding it should work? Maybe I should ditch the information that is imported and make it manual labour. Maybe this is not to be automated?

#1360045

I think it should work, but you should try a small-scale experiment to be sure. There are other variables to consider.

I would not rely on automated address field information - it is not specific or unique enough. It is best to select a location from the auto-suggested options, or validate each entry some other way with the Maps API and your own custom code.

#1360059

Wim

Hi Christian,

Just did some further testing on a staging environment and removing the code that copies the data in the toolset field and then manually update the geolocation fixes all problems so far. Will do some further testing but thinks look good now. I would like to thank you very much for thinking along and guiding me to the cause and the solution of this problem! I will remove access to the site now.

Thanks again and have a good day!

Kind regards,

Wim

#1360061

Wim

My issue is resolved now. Thank you very much Christian!