Answers.Body: <p>You could use the <code>relatedTo</code> parameter (<a href="https://docs.craftcms.com/v3/relations.html" rel="nofollow noreferrer">docs</a>) to query recipes related to the entry that has your <em>Entries</em> field on. Something like:</p>
  
  <pre class="lang-php prettyprint-override"><code>return [
      'elementType' =&gt; Entry::class,
      'criteria' =&gt; [
          'section' =&gt; 'recipes',
          'relatedTo' =&gt; ['sourceElement' =&gt; $entryWithEntriesField]
  ],
  </code></pre>
  
  <p>Where <code>$entryWithEntriesField</code> is the entry (or <code>id</code> of the entry) that contains the entry selection field would return the list of recipes attached to that entry.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-09T19:00:50.273Z
Answers.Text: Thank you - that works perfect.
Answers.UserDisplayName: io_berlin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-09T18:39:29.537Z
Answers.LastActivityDate: 2019-12-09T18:39:29.537Z
Answers.UserDisplayName: Oli
Body: <p>I recently started using the Elements API and I made up a recipe site as an example project. It's pretty easy to output the recipes as json data.</p>
  
  <p>But I now want some more control about the output of the data and only query a specific subset of the recipes. As long as I have the data needed for the selection directly in the recipes entries it's easy to filter or sort them accordingly. However sometimes I might want to manually pick and order entries. One solution would be to create extra attributes in the recipe entries, but this seems messy.</p>
  
  <p>I would like to do that with an entry selection field since this seems to be the most intuitive option for content authors. My question is how can I use the data of an entry field to limit the output? My current solutions seem really bad:</p>
  
  <ol>
  <li><p>I simply query each entry separately by id, but this seems like a terrible idea in particular for a larger selection.</p></li>
  <li><p>I load all data and do the cleanup in Javascript. Even though the images at least wouldn't get loaded this way it seems like an increasingly bad idea with an increasing number of total entries.</p></li>
  <li><p>The best solution for now seems to create an endpoint for the entry that contains the entry selection field and use the transformer to output the all the needed data - I miss the nice pagination / meta data that way however.</p></li>
  </ol>
  
  <pre><code>'transformer' =&gt; function(Entry $entry) {
       $selection =  [];
       foreach ($entry-&gt;selectedRecipes as $block) {
            $selection[] = [ array(
                            'title' =&gt; $block-&gt;title,
                            'id' =&gt; $block-&gt;id,
                            ...
                             )
                           ];
               }
       return [ 'recipes' =&gt; $selection,
  </code></pre>
  
  <p>This seems like a really basic problem and any hints on how to solve this problem are greatly appreciated.</p>
  
  <p>Thank you</p>
  
Tags: <plugin-elementapi><elementcriteriamodel>
Title: Elements API - How to order / limit the output with an asset field of an entry
ThreadText: # Elements API - How to order / limit the output with an asset field of an entry
  
  (Asked by: username_0 on 2019-12-09)
  
  I recently started using the Elements API and I made up a recipe site as an example project. It's pretty easy to output the recipes as json data.
  
  But I now want some more control about the output of the data and only query a specific subset of the recipes. As long as I have the data needed for the selection directly in the recipes entries it's easy to filter or sort them accordingly. However sometimes I might want to manually pick and order entries. One solution would be to create extra attributes in the recipe entries, but this seems messy.
  
  I would like to do that with an entry selection field since this seems to be the most intuitive option for content authors. My question is how can I use the data of an entry field to limit the output? My current solutions seem really bad:
  
  1. I simply query each entry separately by id, but this seems like a terrible idea in particular for a larger selection.
  2. I load all data and do the cleanup in Javascript. Even though the images at least wouldn't get loaded this way it seems like an increasingly bad idea with an increasing number of total entries.
  3. The best solution for now seems to create an endpoint for the entry that contains the entry selection field and use the transformer to output the all the needed data - I miss the nice pagination / meta data that way however.
  
  ```
  'transformer' => function(Entry $entry) {
       $selection =  [];
       foreach ($entry->selectedRecipes as $block) {
            $selection[] = [ array(
                            'title' => $block->title,
                            'id' => $block->id,
                            ...
                             )
                           ];
               }
       return [ 'recipes' => $selection,
  
  ```
  
  This seems like a really basic problem and any hints on how to solve this problem are greatly appreciated.
  
  Thank you
  
  **Tags:** plugin-elementapi, elementcriteriamodel
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-12-09. Score: 1 | Accepted answer
  
  You could use the `relatedTo` parameter ([docs](https://docs.craftcms.com/v3/relations.html)) to query recipes related to the entry that has your *Entries* field on. Something like:
  
  ```php
  return [
      'elementType' => Entry::class,
      'criteria' => [
          'section' => 'recipes',
          'relatedTo' => ['sourceElement' => $entryWithEntriesField]
  ],
  
  ```
  
  Where `$entryWithEntriesField` is the entry (or `id` of the entry) that contains the entry selection field would return the list of recipes attached to that entry.
  
  > **Comments:**
  > **username_0**: Thank you - that works perfect.  
  
  ---
  
  