<p>First, you should get all the Merchant Entries with the merchantId of 123</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section('merchants');
$criteria-&gt;merchantId = '123';

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

<p>Next, find all Gift Card entries that are related to the found merchants.</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'giftCards';
$criteria-&gt;relatedTo = array(
    'targetElement' =&gt; $merchants,
    'field' =&gt; 'YOUR-ENTRIES-FIELD'
);

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

CC BY-SA 3.0
2015-03-30T14:06:36.983Z
This is exactly how I ended up getting it to work.  I think I had some parameters switched around the first time through.
Conor
CC BY-SA 3.0
2015-03-29T21:38:39.387Z
2015-03-29T21:38:39.387Z
Aaron Berkowitz
<p>I have 2 sections: Gift Cards and Merchants.</p>

<p>In Gift Cards, I have a entries field that is limited to the Merchants section and limited to 1 selection. </p>

<p>In Merchants, I have a field merchantId that is a plaintextfield.</p>

<p>In my plugin, I would like to return all gift cards that have a merchantId of 123.  I've tried a few different ways, but all ways have either given no results or I get a 'missing index Merchant' error in the FieldServices class. </p>

<p>Hopefully someone here has done something similar and can point me in the right direction.</p>

<entries><plugins>
Get related entries in plugin via Entries field type
# Get related entries in plugin via Entries field type

(Asked by: username_0 on 2015-03-29)

I have 2 sections: Gift Cards and Merchants.

In Gift Cards, I have a entries field that is limited to the Merchants section and limited to 1 selection.

In Merchants, I have a field merchantId that is a plaintextfield.

In my plugin, I would like to return all gift cards that have a merchantId of 123. I've tried a few different ways, but all ways have either given no results or I get a 'missing index Merchant' error in the FieldServices class.

Hopefully someone here has done something similar and can point me in the right direction.

**Tags:** entries, plugins


---

## 1 answers

### Answer by username_1 on 2015-03-29. Score: 8 | Accepted answer

First, you should get all the Merchant Entries with the merchantId of 123
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section('merchants');
$criteria->merchantId = '123';

$merchants = $criteria->find();

Next, find all Gift Card entries that are related to the found merchants.
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'giftCards';
$criteria->relatedTo = array(
    'targetElement' => $merchants,
    'field' => 'YOUR-ENTRIES-FIELD'
);

$gift_cards = $criteria->find();

> **Comments:**
> **username_0**: This is exactly how I ended up getting it to work.  I think I had some parameters switched around the first time through.  

---

