<p>You’d do this with a brand new element query using the <a href="https://craftcms.com/docs/templating/craft.entries#relatedTo" rel="noreferrer">relatedTo</a> param.</p>



<p>Craft 2:</p>

<pre class="lang-php prettyprint-override"><code>// This is the PHP equivalent of typing `craft.entries()` in a template:
$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);

// Only fetch Location entries that are related to the Site entry:
$criteria-&gt;section = 'locations';
$criteria-&gt;relatedTo = ['targetElement' =&gt; $entry];

$locations = $criteria-&gt;find();
</code></pre>

<p>Craft 3:</p>

<pre class="lang-php prettyprint-override"><code>// This is the PHP equivalent of typing `craft.entries()` in a template:
$query = \craft\elements\Entry::find();

// Only fetch Location entries that are related to the Site entry:
$query-&gt;section('locations');
$query-&gt;relatedTo(['targetElement' =&gt; $entry]);

$locations = $query-&gt;all();
</code></pre>

CC BY-SA 3.0
2017-12-01T22:33:05.363Z
Looks like this broke in the updated version we're using for Craft 3. Getting a _Call to undefined function craft()_ error when loading up the endpoint.
Roger Erik Tinch
CC BY-SA 3.0
2017-12-02T14:09:41.577Z
@Roger you might want to read the documentation  https://github.com/craftcms/docs/blob/master/en/element-queries.md you'll find many things you need to know for Craft 3 but if you are going to create a more complex site (with custom plugins) you must go through the source code or analyze other plugins in order to use everything properly
Robin Schambach
CC BY-SA 3.0
2016-03-31T19:32:31.667Z
2017-12-02T13:55:51.603Z
2017-12-02T13:55:51.603Z
Brandon Kelly
<p>I have two sections, each with a single entry type:</p>

<ul>
<li>Sites</li>
<li>Locations</li>
</ul>

<p>In the Locations entry type I have a Entries field called Site that you can use to relate a Location to a Site entry.</p>

<p>My questions is: how do I show which locations were related to a Site entry when using the Element API? I'd like to display it as an array when using a sites/.json endpoint.</p>

<p>This is what I've tried, but it breaks the endpoint:</p>

<pre><code>'v1/sites/&lt;entryId:\d+&gt;.json' =&gt; function($entryId) {
  return [
    'elementType' =&gt; 'Entry',
    'criteria' =&gt; ['id' =&gt; $entryId],
    'first' =&gt; true,
    'transformer' =&gt; function(EntryModel $entry) {
      $locations = $entry-&gt;getChildren()-&gt;type('locations')-&gt;find();
      foreach ($locations as $location) {
        $locationsInfo[] = [
          'name' =&gt; $location-&gt;title
        ];
      }

      return [
        'id' =&gt; $entry-&gt;id,
        'name' =&gt; $entry-&gt;title,
        'tagline' =&gt; $entry-&gt;tagline,
        'about' =&gt; $entry-&gt;about-&gt;getParsedContent(),
        'locations' =&gt; $locationsInfo,
        'jsonUrl' =&gt; UrlHelper::getUrl("v1/sites/{$entry-&gt;id}.json")
      ];
    },
  ];
},
</code></pre>

<plugin-elementapi>
Calling Related Entries using the Element API
# Calling Related Entries using the Element API

(Asked by: username_0 on 2016-03-31)

I have two sections, each with a single entry type:

Sites
Locations

In the Locations entry type I have a Entries field called Site that you can use to relate a Location to a Site entry.
My questions is: how do I show which locations were related to a Site entry when using the Element API? I'd like to display it as an array when using a sites/.json endpoint.
This is what I've tried, but it breaks the endpoint:
'v1/sites/.json' => function($entryId) {
  return [
    'elementType' => 'Entry',
    'criteria' => ['id' => $entryId],
    'first' => true,
    'transformer' => function(EntryModel $entry) {
      $locations = $entry->getChildren()->type('locations')->find();
      foreach ($locations as $location) {
        $locationsInfo[] = [
          'name' => $location->title
        ];
      }

      return [
        'id' => $entry->id,
        'name' => $entry->title,
        'tagline' => $entry->tagline,
        'about' => $entry->about->getParsedContent(),
        'locations' => $locationsInfo,
        'jsonUrl' => UrlHelper::getUrl("v1/sites/{$entry->id}.json")
      ];
    },
  ];
},

**Tags:** plugin-elementapi

---

## 1 answers

### Answer by username_1 on 2016-03-31. Score: 9 | Accepted answer

You’d do this with a brand new element query using the relatedTo param.
Craft 2:
// This is the PHP equivalent of typing `craft.entries()` in a template:
$criteria = craft()->elements->getCriteria(ElementType::Entry);

// Only fetch Location entries that are related to the Site entry:
$criteria->section = 'locations';
$criteria->relatedTo = ['targetElement' => $entry];

$locations = $criteria->find();

Craft 3:
// This is the PHP equivalent of typing `craft.entries()` in a template:
$query = \craft\elements\Entry::find();

// Only fetch Location entries that are related to the Site entry:
$query->section('locations');
$query->relatedTo(['targetElement' => $entry]);

$locations = $query->all();

> **Comments:**
> **username_0**: Looks like this broke in the updated version we're using for Craft 3. Getting a _Call to undefined function craft()_ error when loading up the endpoint.  
> **username_2**: @Roger you might want to read the documentation  https://github.com/craftcms/docs/blob/master/en/element-queries.md you'll find many things you need to know for Craft 3 but if you are going to create a more complex site (with custom plugins) you must go through the source code or analyze other plugins in order to use everything properly  

---

