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.


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

    'targetElement' => $article,
    'field' => 'articleSections.articles'
$page = Entry::find()
        'targetElement' => $article, 
        'field'         => 'articleSections.articles' 

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

| improve this answer | |
  • 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 Jun 11 '18 at 11:09
  • My code should exactly cover your case. Is your section correct? and do you fetch the correct $article – Robin Schambach Jun 11 '18 at 11:12
  • Yes 'page' is the handle of the Section and it is the correct $article. Do you need any more info from me? – biko Jun 11 '18 at 11:18
  • 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 Jun 11 '18 at 11:20
  • 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 Jun 11 '18 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.