Sorting entries by closest locations

Smart Map has been replaced by Google Maps

Please install the Google Maps plugin instead. ➡️

As of February 2021, the Smart Map plugin has been completely rebuilt and replaced with the new Google Maps plugin for Craft CMS. For more details, see here...

The documentation below is for historical reference only.

# Twig template

Include this in your craft.entries call:


This will tell Craft to:

  • Filter your Address field with the parameters you specify.
  • Order the results by closest distance from your specified target.
{# #}

{% set target = craft.request.getParam('near') %}
{% set params = {
    target: target,
    range: 100
} %}

{% set entries = craft.entries.myFieldHandle(params).orderBy('distance').all() %}

<h1>Showing results for "{{ target }}"...</h1>
{% for entry in entries %}
    <h2>{{ entry.title }}</h2>
        {{ entry.myFieldHandle.street1 }}<br />
        {{ entry.myFieldHandle.street2 }}<br />
        {{ }}, {{ entry.myFieldHandle.state }} {{ }}<br>
        <strong>{{ entry.myFieldHandle.distance | number_format(1) }} miles away</strong>
{% else %}
    <h2>No results found</h2>
{% endfor %}

Your target can be anything that translates into a full or partial address...

  • 90210
  • Aurora, IL
  • 742 Evergreen Terrace

Or you can even use specific coordinates (lat,lng) as your target...

  • 38.897659,-77.036587

All parameters are optional.

Parameter Default Description
target (autodetects) Starting point for proximity search
range 25 Search radius, measured in units
units miles Units of measurement (miles or kilometers)

# PHP in a separate plugin or module

This operates very similarly to how it's done in Twig:

$entries = Entry::find()
        'target' => 90210,
        'range' => 100