Set region status by a value of a custom ACF field
Set region status by ACF field
Section titled “Set region status by ACF field”If Regions are linked to WordPress posts that have an ACF field, you can map that field’s values to MapSVG Region Status values (colors / enabled state).
Prerequisites
Section titled “Prerequisites”- ACF installed and active
- Regions connected to WordPress posts (WP Post field or equivalent setup in your map)
- A Status field configured under Regions › Edit fields
- You know the ACF field name and which ACF values should map to which MapSVG status IDs
- Open the map in the Map Editor
- Go to Menu › JavaScript › Map › afterLoadRegions
- Replace the handler with the code below
- Set
statusFieldNameto your ACF field name - Edit
acfToMapsvgStatusesso each ACF value maps to a MapSVG status ID - Save and reload the front end
function (event){
const { map } = event
const acfToMapsvgStatuses = { // ACF field value → MapSVG status ID "1": 1, "2": 2, "3": 3 };
map.regionsRepository.getLoaded().forEach( function(regionDb){ if(regionDb.post && regionDb.post.acf){ var status = acfToMapsvgStatuses[regionDb.post.acf.statusFieldName]; if(typeof status !== "undefined"){ const regionSvg = map.getRegion(regionDb.id) if(regionSvg){ regionSvg.setStatus(status) }
} } });
}Replace statusFieldName in regionDb.post.acf.statusFieldName with your real ACF field key.