<p>Being that the <code>smartMapField</code> attribute belongs to entries in the <code>Property</code> section, and not the entries in the <code>Unit</code> section, your current element criteria is a bit off (it basically assumes that the <code>smartMapField</code> property is found on the <code>Unit</code> entry models).</p>

<p>Basically, you'll need <em>two</em> element criterias to accomplish what you want. </p>

<p>The first criteria will pull <code>Property</code> entries based on their <code>smartMapField</code> value and the <code>params</code> object. The second one will pull <code>Unit</code> properties, by relation to the <code>Property</code> entries in the first criteria.</p>

<p>Finally, we'll need to use the <code>group</code> filter to output the unit entries in the correct order (i.e. those related to the nearest property entry first).</p>

<p>Important: It isn't 100% clear from your question, but I'm assuming that the relation between <code>Units</code> and <code>Properties</code> originates from a <code>relatedPropertyField</code> in the <code>Units</code> entries, rather than the other way around.</p>

<p>I haven't been able to test this, but I believe something like this should do the trick:</p>

<pre><code>{% set nearest = craft.entries.section('units').relatedTo({
    targetElement : craft.entries.section('properties').smartMapField(params).order('distance').limit(null),
    field : 'relatedPropertyField'
}).limit(null).order('targets1.sortOrder')|group('relatedPropertyField.first().id') %}
</code></pre>

<p>Note the <code>group</code> filter applied to the <code>nearest</code> variable. This is needed to group the returned entries by their common <code>Property</code> relation. The <code>group</code> filter turns <code>nearest</code> into an array of grouped elements, which means that when you output the entries, you'll need a nested <code>for</code> loop:</p>

<pre><code>{% for units in nearest %}
    {% for unit in units %}
        ...
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-11-15T10:57:41.627Z
2015-11-15T12:53:42.047Z
2015-11-15T12:53:42.047Z
Mats Mikkel Rummelhoff
<p>I’m trying to build a query to get the geographically closest entries, based on an Smart Map field value within a related entry.</p>

<p><a href="https://i.sstatic.net/FBEq6.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/FBEq6.png" alt="Diagram of relationships and template"></a></p>

<p>The following template tags were my first guess, but returned an empty result set.</p>

<pre><code>{% set nearest = allUnitsCriteria.relatedPropertyField.smartMapField(params).order('distance') %}
</code></pre>

<p>What’s the best way to do this?</p>

CC BY-SA 3.0
2015-11-14T23:47:23.590Z
Are you referring to the [Smart Map](https://plugins.doublesecretagency.com/smart-map/) plugin? If so, your code looks correct as per [the example in the docs](https://plugins.doublesecretagency.com/smart-map/sorting-entries-by-closest-locations/) (assuming field handles are correct). Have you tested whether `allUnitsCriteria` actually returns entries without the `smartAddressField` argument? What happens if you dump `allUnitsCriteria|length`? Also, what does the `params` object you pass to `smartAddressField` contain?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-11-15T10:28:20.637Z
Thanks, @mmikkel. This is indeed a Smart Map field.

The `allUnitsCriteria` is just a simplification. At present, it’s set to `craft.entries.section('units').limit(null)`. `params` are set to `{ target: '_lat_,_lng_', range: 1000 }`.

The complication here is that the **Unit** entry doesn’t contain an address field, but instead links to a **Property** entry, which does contain an address field.
Joshua Baker
<relations><plugin-smartmap>
Nearest entry using related entry Smart Map field
# Nearest entry using related entry Smart Map field

(Asked by: username_0 on 2015-11-14)

I’m trying to build a query to get the geographically closest entries, based on an Smart Map field value within a related entry.

The following template tags were my first guess, but returned an empty result set.
{% set nearest = allUnitsCriteria.relatedPropertyField.smartMapField(params).order('distance') %}

What’s the best way to do this?

**Tags:** relations, plugin-smartmap
> **Comments:**
> **username_1**: Are you referring to the [Smart Map](https://plugins.doublesecretagency.com/smart-map/) plugin? If so, your code looks correct as per [the example in the docs](https://plugins.doublesecretagency.com/smart-map/sorting-entries-by-closest-locations/) (assuming field handles are correct). Have you tested whether `allUnitsCriteria` actually returns entries without the `smartAddressField` argument? What happens if you dump `allUnitsCriteria|length`? Also, what does the `params` object you pass to `smartAddressField` contain?  
> **username_0**: Thanks, @mmikkel. This is indeed a Smart Map field.  The `allUnitsCriteria` is just a simplification. At present, it’s set to `craft.entries.section('units').limit(null)`. `params` are set to `{ target: '_lat_,_lng_', range: 1000 }`.  The complication here is that the **Unit** entry doesn’t contain an address field, but instead links to a **Property** entry, which does contain an address field.  

---

## 1 answers

### Answer by username_1 on 2015-11-15. Score: 3

Being that the smartMapField attribute belongs to entries in the Property section, and not the entries in the Unit section, your current element criteria is a bit off (it basically assumes that the smartMapField property is found on the Unit entry models).
Basically, you'll need two element criterias to accomplish what you want. 
The first criteria will pull Property entries based on their smartMapField value and the params object. The second one will pull Unit properties, by relation to the Property entries in the first criteria.
Finally, we'll need to use the group filter to output the unit entries in the correct order (i.e. those related to the nearest property entry first).
Important: It isn't 100% clear from your question, but I'm assuming that the relation between Units and Properties originates from a relatedPropertyField in the Units entries, rather than the other way around.
I haven't been able to test this, but I believe something like this should do the trick:
{% set nearest = craft.entries.section('units').relatedTo({
    targetElement : craft.entries.section('properties').smartMapField(params).order('distance').limit(null),
    field : 'relatedPropertyField'
}).limit(null).order('targets1.sortOrder')|group('relatedPropertyField.first().id') %}

Note the group filter applied to the nearest variable. This is needed to group the returned entries by their common Property relation. The group filter turns nearest into an array of grouped elements, which means that when you output the entries, you'll need a nested for loop:
{% for units in nearest %}
    {% for unit in units %}
        ...
    {% endfor %}
{% endfor %}


---

