I want to sort entries with a Smart Map address field for closest location. I know how it should be done in a template:

{% set entries = craft.entries.myAddress(params).order('distance').find() %}

Only, I would like to do so in my business logic and not in my template. I tried something like this.

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'houses';
$criteria->myAddress->target = '90210';
$criteria->order = 'myAddress.distance';

However, this does not work.

Is this possible? I tried related options but can't seem to get anything to work. Can I also do this with the Element API plugin to return the results as JSON?

2 Answers 2


You're very close!

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->myFieldHandle = array(
    'target' => 90210,
    'range' => 100
$criteria->order = 'distance';

$results = $criteria->find();

I've never actually tested it with the Element API plugin, but in theory it should work just fine.

In response to this question, I've added instructions for PHP to the docs.


Just figured out how to actually use the Smart Map plugin with the Element API plugin:

'endpoints' => [
    'entries.json' => [
        'criteria' => [
            'section' => 'sectionHandle',
            'address' => array(
                'target' => '50.25641910,6.10379780',
                'range' => 20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.