Sort objects by ACF field
Sort Database objects loaded on the map (and in the Directory) by a value from an Advanced Custom Fields (ACF) field on the linked WordPress post.
Prerequisites
- ACF installed and active
- MapSVG Database objects linked to WordPress posts that have the ACF field
- You know the ACF field name (as registered in ACF)
Steps
- Open the map in the Map Editor
- Go to Menu › JavaScript › Map › afterLoadObjects
- Replace the handler with the code below
- Change
fieldNameto your ACF field name - Save the map and reload the front end
function(event){
const { map } = event
var fieldName = 'fieldName'
map.database.getLoaded().sort((objA, objB) => objA.post.acf[fieldName] > objB.post.acf[fieldName] ? 1 : -1)
}
info
afterLoadObjects runs after objects are loaded. Sorting only affects the currently loaded page of results if pagination is enabled.