<p>The "proper" way to access the element query that gets generated is through a plugin via the <a href="https://github.com/craftcms/cms/blob/develop/src/elements/db/ElementQuery.php#L79-L85" rel="nofollow noreferrer">ElementQuery's <code>query</code> and <code>subquery</code></a> properties.</p>

<p>It looks like you're doing a Haversine algorithm to calculate distance between a latitude and longitude.  There's are some similar plugins that already do this you can use as an example.</p>

<p>For example: <a href="https://github.com/ethercreative/simplemap/blob/v3/src/services/MapService.php" rel="nofollow noreferrer">https://github.com/ethercreative/simplemap/blob/v3/src/services/MapService.php</a></p>

CC BY-SA 3.0
2018-02-08T21:07:31.327Z
2018-02-08T21:07:31.327Z
Brad Bell
<p>I've had to manually do an SQL call to the get correct results but I was hoping there would be a tidier way of achieving this in Craft 3?</p>

<pre><code>$query = ( new Query() )
-&gt;select('craft_content.title, craft_entries.id, ( 3959 * acos( cos( radians(' . $latitude . ') ) * cos( radians( field_clubLatitude ) ) * 
            cos( radians( field_clubLongitude ) - radians(' . $longitude . ') ) + sin( radians(' . $latitude . ') ) * 
            sin( radians( field_clubLatitude ) ) ) ) AS distance')
-&gt;from( 'craft_elements' )
-&gt;where('field_clubLongitude != "" and field_clubLatitude != ""')
-&gt;join( 'LEFT JOIN', 'craft_entries', 'craft_elements.id = craft_entries.id' )
-&gt;join( 'LEFT JOIN', 'craft_content', 'craft_elements.id = craft_content.elementId' )
-&gt;andHaving('distance &lt; 100')
-&gt;orderBy('distance')
-&gt;limit(3);
</code></pre>

CC BY-SA 3.0
2018-02-08T16:06:03.930Z
Could you please explain what you want to achieve in detail? Should every query have these additional selects? Do you need it only once? Do you need system wide queries? Do you only need those additions when you query or should other team members be able to attach them with `craft.entries` too?

The most easiest way is to create a custom query class that extends the entries query and use that in your twig template. Thus you would do `craft.plugin.entries` to return a custom query class
Robin Schambach
<entry><craft3><element>
Anyway to extend Craft Entry query search in Craft 3
# Anyway to extend Craft Entry query search in Craft 3

(Asked by: username_0 on 2018-02-08)

I've had to manually do an SQL call to the get correct results but I was hoping there would be a tidier way of achieving this in Craft 3?
$query = ( new Query() )
->select('craft_content.title, craft_entries.id, ( 3959 * acos( cos( radians(' . $latitude . ') ) * cos( radians( field_clubLatitude ) ) * 
            cos( radians( field_clubLongitude ) - radians(' . $longitude . ') ) + sin( radians(' . $latitude . ') ) * 
            sin( radians( field_clubLatitude ) ) ) ) AS distance')
->from( 'craft_elements' )
->where('field_clubLongitude != "" and field_clubLatitude != ""')
->join( 'LEFT JOIN', 'craft_entries', 'craft_elements.id = craft_entries.id' )
->join( 'LEFT JOIN', 'craft_content', 'craft_elements.id = craft_content.elementId' )
->andHaving('distance < 100')
->orderBy('distance')
->limit(3);

**Tags:** entry, craft3, element

> **Comments:**
> **username_1**: Could you please explain what you want to achieve in detail? Should every query have these additional selects? Do you need it only once? Do you need system wide queries? Do you only need those additions when you query or should other team members be able to attach them with `craft.entries` too?  The most easiest way is to create a custom query class that extends the entries query and use that in your twig template. Thus you would do `craft.plugin.entries` to return a custom query class  

---

## 1 answers

### Answer by username_2 on 2018-02-08. Score: 1

The "proper" way to access the element query that gets generated is through a plugin via the [ElementQuery's `query` and `subquery`](https://github.com/craftcms/cms/blob/develop/src/elements/db/ElementQuery.php#L79-L85) properties.

It looks like you're doing a Haversine algorithm to calculate distance between a latitude and longitude. There's are some similar plugins that already do this you can use as an example.

For example: <https://github.com/ethercreative/simplemap/blob/v3/src/services/MapService.php>


---

