<pre><code>$page = Entry::find()
    -&gt;relatedTo([
        'targetElement' =&gt; $article, 
        'field'         =&gt; 'articleSections.articles' 
    ])
    -&gt;section('page')-&gt;all();
</code></pre>

<p>Please read the docs about <a href="https://docs.craftcms.com/v3/element-queries.html" rel="nofollow noreferrer">ElementQueries</a> and <a href="https://docs.craftcms.com/v3/relations.html#the-relatedto-parameter" rel="nofollow noreferrer">relations</a></p>

<p>Note: the Element API Creates an <code>ElementQuery</code> from your <code>criteria</code> property.</p>

<pre><code>criteria =&gt; [
    'sectionId' =&gt; 4,
    'typeId'    =&gt; 1
]
</code></pre>

<p>Is the same as</p>

<pre><code>Entry::find()-&gt;sectionId(4)-&gt;typeId(1)
</code></pre>

CC BY-SA 4.0
2018-06-11T11:09:15.167Z
Thanks for your answer!  However, using your query, $page returns an empty object {}.  I can confirm that $article is a craft\elements\Entry object.  Also note that the Entries field is found on the 'page' entry and that the field selects 'article' entries.  Could this be the reason why it doesn't work here?  Also please note the 'page' is a Section of type Structure.
biko
CC BY-SA 4.0
2018-06-11T11:12:35.777Z
My code should exactly cover your case. Is your section correct? and do you fetch the correct `$article`
Robin Schambach
CC BY-SA 4.0
2018-06-11T11:18:26.333Z
Yes 'page' is the handle of the Section and it is the correct $article.  Do you need any more info from me?
biko
CC BY-SA 4.0
2018-06-11T11:20:30.960Z
One more thing.  In the 'page' entry, I have Matrix blocks which contain the article 'Entries' field.  Could this be an issue?  Could the relationship be set between the matrix blocks and 'article' entries, instead of the 'page' and 'article' entries?
biko
CC BY-SA 4.0
2018-06-11T11:26:04.463Z
I just tested adding the Entries field to the 'page' entry itself and NOT the Matrix blocks as stated in my previous comments and it worked.  So the problem here is that relationship is being set to the Matrix blocks and the 'article' entries.  Can I solve this in one query or should I loop over each Matrix block and query the relationship?
biko
CC BY-SA 4.0
2018-06-11T11:48:39.290Z
Sorry for another comment.  I'm trying to refer to the relations documentation and came up with the following query, but it's failing... can you help?
$pages = Entry::find()->section('page')->relatedTo([
 'sourceElement' => $article,
 'field' => 'articleSections.articles'
])->all();
biko
CC BY-SA 4.0
2018-06-11T12:17:44.817Z
Ok I found the solution.  The correct param was targetElement instead of sourceElement.  So the correct query is $pages = Entry::find()->section('page')->relatedTo([ 'targetElement' => $article, 'field' => 'articleSections.articles' ])->all();  I'm going to edit the question to be more accurate.  If you update your answer, I'll mark it as correct.  Thanks for pointing me to the correct path and docs!
biko
CC BY-SA 4.0
2018-06-11T12:26:32.450Z
Indeed. I assumed you related your elements directly, if you don't insert a field it will search for all fields (it's faster to describe that scenario and most people prefer the short syntax - but if you are going to relate entries with matrix fields you need to use the long syntax)
Robin Schambach
CC BY-SA 4.0
2018-06-11T09:43:27.467Z
2018-06-11T12:27:43.527Z
2018-06-11T12:27:43.527Z
Robin Schambach
<p>In a Matrix block field of a "page" entry, I'm using an <code>Entries field</code> to manually select a list of "article" entries to the "page" entry.</p>

<p>When I query an "article" entry, is it possible to query its "parent" page entry that's connected using the above method or do I have to use categories?</p>

<p>BTW it'd be appreciated if the solution is written for the Element API plugin or PHP code if possible.</p>

<p>Thanks!</p>

<p>EDIT: edited question to be more accurate by adding the Matrix details.  Also, the correct query would be:</p>

<pre><code>Entry::find()-&gt;section('page')-&gt;relatedTo([
    'targetElement' =&gt; $article,
    'field' =&gt; 'articleSections.articles'
])-&gt;all();
</code></pre>

<craft3><relations><plugin-elementapi><relatedto>
Craft 3 Element API: Query the "parent" entry of a "child" entry with Matrix blocks connected by an 'Entries' field
# Craft 3 Element API: Query the "parent" entry of a "child" entry with Matrix blocks connected by an 'Entries' field

(Asked by: username_0 on 2018-06-11)

In a Matrix block field of a "page" entry, I'm using an Entries field to manually select a list of "article" entries to the "page" entry.
When I query an "article" entry, is it possible to query its "parent" page entry that's connected using the above method or do I have to use categories?
BTW it'd be appreciated if the solution is written for the Element API plugin or PHP code if possible.
Thanks!
EDIT: edited question to be more accurate by adding the Matrix details.  Also, the correct query would be:
Entry::find()->section('page')->relatedTo([
    'targetElement' => $article,
    'field' => 'articleSections.articles'
])->all();

**Tags:** craft3, relations, plugin-elementapi, relatedto

---

## 1 answers

### Answer by username_1 on 2018-06-11. Score: 3 | Accepted answer

$page = Entry::find()
    ->relatedTo([
        'targetElement' => $article, 
        'field'         => 'articleSections.articles' 
    ])
    ->section('page')->all();

Please read the docs about ElementQueries and relations
Note: the Element API Creates an ElementQuery from your criteria property.
criteria => [
    'sectionId' => 4,
    'typeId'    => 1
]

Is the same as
Entry::find()->sectionId(4)->typeId(1)

> **Comments:**
> **username_0**: Thanks for your answer!  However, using your query, $page returns an empty object {}.  I can confirm that $article is a craft\elements\Entry object.  Also note that the Entries field is found on the 'page' entry and that the field selects 'article' entries.  Could this be the reason why it doesn't work here?  Also please note the 'page' is a Section of type Structure.  
> **username_1**: My code should exactly cover your case. Is your section correct? and do you fetch the correct `$article`  
> **username_0**: Yes 'page' is the handle of the Section and it is the correct $article.  Do you need any more info from me?  
> **username_0**: One more thing.  In the 'page' entry, I have Matrix blocks which contain the article 'Entries' field.  Could this be an issue?  Could the relationship be set between the matrix blocks and 'article' entries, instead of the 'page' and 'article' entries?  
> **username_0**: I just tested adding the Entries field to the 'page' entry itself and NOT the Matrix blocks as stated in my previous comments and it worked.  So the problem here is that relationship is being set to the Matrix blocks and the 'article' entries.  Can I solve this in one query or should I loop over each Matrix block and query the relationship?  
> **username_0**: Sorry for another comment.  I'm trying to refer to the relations documentation and came up with the following query, but it's failing... can you help? $pages = Entry::find()->section('page')->relatedTo([  'sourceElement' => $article,  'field' => 'articleSections.articles' ])->all();  
> **username_0**: Ok I found the solution.  The correct param was targetElement instead of sourceElement.  So the correct query is $pages = Entry::find()->section('page')->relatedTo([ 'targetElement' => $article, 'field' => 'articleSections.articles' ])->all();  I'm going to edit the question to be more accurate.  If you update your answer, I'll mark it as correct.  Thanks for pointing me to the correct path and docs!  
> **username_1**: Indeed. I assumed you related your elements directly, if you don't insert a field it will search for all fields (it's faster to describe that scenario and most people prefer the short syntax - but if you are going to relate entries with matrix fields you need to use the long syntax)  

---

