<p>You're very close! Try this:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'concepts';
$criteria-&gt;limit   = null;
$criteria-&gt;relatedTo = array(
    'targetElement' =&gt; $e,
    'field'         =&gt; $field
);
$elements = craft()-&gt;elements-&gt;findElements($criteria);
</code></pre>

<p>Keep in mind, in this instance, $field is the field object which can be retrieved with something like the following:</p>

<pre><code>$singleFieldModel = craft()-&gt;fields-&gt;getFieldById($singleField);
</code></pre>

<p>See the documentation (<a href="https://craftcms.com/classreference/services/FieldsService" rel="nofollow noreferrer">https://craftcms.com/classreference/services/FieldsService</a>) for other ways to get a field object.</p>

CC BY-SA 3.0
2014-09-03T14:27:05.710Z
Thanks! Even more valuable than the correction solution was the link to https://craftcms.com/classreference! No more grep'ing the source code!
gromgull
CC BY-SA 3.0
2014-09-03T14:28:04.483Z
Totally. P&T released that a few weeks ago and it's been pretty invaluable.
Peter Tell
CC BY-SA 3.0
2017-07-08T16:09:08.897Z
Don't know if this has changed since you posted it, but now it seems `field` should get the field handle, and not the field object.
KnutSv
CC BY-SA 3.0
2014-09-03T13:34:34.200Z
2017-07-25T06:08:39.093Z
2017-07-25T06:08:39.093Z
Peter Tell
<p>In Twig code you can use <code>craft-&gt;entries-&gt;relatedTo</code> to do all sorts of neat things. </p>

<p>From plugin code I am not sure how this works? </p>

<pre><code>$c = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$c-&gt;section = 'concepts';
$c-&gt;relatedTo(array('targetElement'=&gt;$e, 'field'=&gt;'entry'));
$concepts = $c-&gt;find();
</code></pre>

<p>This doesn't throw an error - but it doesn't work either. </p>

<p>How can I use the criteria model to retrieve entries that have some relationship? </p>

<p>Cheers!</p>

<plugin-development><elementcriteriamodel><relations>
How can I follow relations in ElementCriteriaModels from plugin code?
# How can I follow relations in ElementCriteriaModels from plugin code?

(Asked by: username_0 on 2014-09-03)

In Twig code you can use craft->entries->relatedTo to do all sorts of neat things. 
From plugin code I am not sure how this works? 
$c = craft()->elements->getCriteria(ElementType::Entry);
$c->section = 'concepts';
$c->relatedTo(array('targetElement'=>$e, 'field'=>'entry'));
$concepts = $c->find();

This doesn't throw an error - but it doesn't work either. 
How can I use the criteria model to retrieve entries that have some relationship? 
Cheers!

**Tags:** plugin-development, elementcriteriamodel, relations


---

## 1 answers

### Answer by username_1 on 2014-09-03. Score: 6 | Accepted answer

You're very close! Try this:
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'concepts';
$criteria->limit   = null;
$criteria->relatedTo = array(
    'targetElement' => $e,
    'field'         => $field
);
$elements = craft()->elements->findElements($criteria);

Keep in mind, in this instance, $field is the field object which can be retrieved with something like the following:
$singleFieldModel = craft()->fields->getFieldById($singleField);

See the documentation (https://craftcms.com/classreference/services/FieldsService) for other ways to get a field object.

> **Comments:**
> **username_0**: Thanks! Even more valuable than the correction solution was the link to https://craftcms.com/classreference! No more grep'ing the source code!  
> **username_1**: Totally. P&T released that a few weeks ago and it's been pretty invaluable.  
> **username_2**: Don't know if this has changed since you posted it, but now it seems `field` should get the field handle, and not the field object.  

---

