Skip to main content

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

  1. Open the map in the Map Editor
  2. Go to Menu › JavaScript › Map › afterLoadObjects
  3. Replace the handler with the code below
  4. Change fieldName to your ACF field name
  5. 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.