Syncing vendor locations with listings is essential for accurate searches and better user experience. Like you, I struggled to sync vendor locations to listings and tried many solutions before finally getting it right. Here’s a detailed guide on how I solved this issue, including the challenges and the code snippet that worked for me.
The Problem
When using the snippet shared in the HivePress community, I noticed an issue:
The code only synced the text of the vendor’s location. As a result, when searching by location, listings wouldn’t appear, even though they had synced locations.
The Initial Code
Here’s the original code I found:
add_filter(
'hivepress/v1/models/listing/attributes',
function($attributes) {
if(isset($attributes['location'])) {
$attributes['location']['synced'] = true;
}
return $attributes;
},
1000
);
While this code enabled basic syncing of location text, it didn’t handle geographical coordinates, which are critical for accurate location-based searches.