Answers.Body: <p>Are you passing the <code>projectCategory</code> parameter as a string or ID? In any case, the relatedTo parameter will receive a string in your code, which might trip up the query. If the parameter is passed as an ID, passing it as an integer instead of a string might be enough:</p>
  <pre class="lang-php prettyprint-override"><code>'relatedTo' =&gt; (int) $projectCategory,
  </code></pre>
  <p>However, it might be cleaner to get the actual category element based on the ID and pass that to <code>relatedTo</code>. This will also allow you to check if the specified category actually exists (and optionally return an HTTP error for invalid parameters).</p>
  <pre class="lang-php prettyprint-override"><code>$criteria = ['section' =&gt; 'projects'];
  $projectCategory = \Craft::$app-&gt;request-&gt;getQueryParam('projectCategory');
  // this assumes the category is passed as an ID
  $category = \craft\elements\Category::find()-&gt;id((int) $projectCategory)-&gt;one();
  if ($category) {
      $criteria['relatedTo'] = $category;
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-20T13:35:22.767Z
Answers.Text: Perfect, thanks for your reply. This works for me!
Answers.UserDisplayName: Malte
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-20T12:21:29.523Z
Answers.LastActivityDate: 2021-05-20T12:21:29.523Z
Answers.UserDisplayName: MoritzLost
Body: <p>I'm using the <a href="https://plugins.craftcms.com/element-api" rel="nofollow noreferrer">Element API Plugin</a> to create an API, with which I want to get entries related to one category, but dynamically with the category set in the Url.</p>
  <p>What I have so far in the <strong>config/element-api.php</strong>:</p>
  <pre><code>&lt;?php
  
  use craft\elements\Entry;
  use craft\helpers\UrlHelper;
  
  return [
    'endpoints' =&gt; [
      'api/projects.json' =&gt; function() {
        $projectCategory = \Craft::$app-&gt;request-&gt;getQueryParam('projectCategory');
  
        // dump for test purpose
        var_dump($projectCategory);
  
        return [
          'elementType' =&gt; Entry::class,
          'criteria' =&gt; [
            'section' =&gt; 'projects',
            'relatedTo' =&gt; &quot;{$projectCategory}&quot;,
          ],
          'elementsPerPage' =&gt; 2,
          'transformer' =&gt; function(Entry $entry) {
            return [
              'title' =&gt; $entry-&gt;title,
              'url' =&gt; $entry-&gt;url,
              'jsonUrl' =&gt; UrlHelper::url(&quot;projects/{$entry-&gt;id}.json&quot;),
              'thumbnail' =&gt; $entry-&gt;projectThumbnail,
              'description' =&gt; $entry-&gt;projectDescription,
              'credits' =&gt; $entry-&gt;projectCredits,
            ];
          },
        ];
      },
    ],
  ];
  </code></pre>
  <p>When I go to <em><a href="http://domain.test/api/projects.json?projectCategory=category" rel="nofollow noreferrer">http://domain.test/api/projects.json?projectCategory=category</a></em>, I get the right value dumped, but the relatedTo doesn't seem to work. I get zero entries, although there are some related to a specific category.</p>
  <p>Am I doing something wrong here?</p>
  
Tags: <categories><plugin-elementapi><filter><query><category-filter>
Title: Element API: Get Entries Related to Category with Query Parameter
ThreadText: # Element API: Get Entries Related to Category with Query Parameter
  
  (Asked by: username_0 on 2021-05-20)
  
  I'm using the [Element API Plugin](https://plugins.craftcms.com/element-api) to create an API, with which I want to get entries related to one category, but dynamically with the category set in the Url.
  
  What I have so far in the **config/element-api.php**:
  
  ```
  <?php
  
  use craft\elements\Entry;
  use craft\helpers\UrlHelper;
  
  return [
    'endpoints' => [
      'api/projects.json' => function() {
        $projectCategory = \Craft::$app->request->getQueryParam('projectCategory');
  
        // dump for test purpose
        var_dump($projectCategory);
  
        return [
          'elementType' => Entry::class,
          'criteria' => [
            'section' => 'projects',
            'relatedTo' => "{$projectCategory}",
          ],
          'elementsPerPage' => 2,
          'transformer' => function(Entry $entry) {
            return [
              'title' => $entry->title,
              'url' => $entry->url,
              'jsonUrl' => UrlHelper::url("projects/{$entry->id}.json"),
              'thumbnail' => $entry->projectThumbnail,
              'description' => $entry->projectDescription,
              'credits' => $entry->projectCredits,
            ];
          },
        ];
      },
    ],
  ];
  
  ```
  
  When I go to *<http://domain.test/api/projects.json?projectCategory=category>*, I get the right value dumped, but the relatedTo doesn't seem to work. I get zero entries, although there are some related to a specific category.
  
  Am I doing something wrong here?
  
  **Tags:** categories, plugin-elementapi, filter, query, category-filter
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-05-20. Score: 3 | Accepted answer
  
  Are you passing the `projectCategory` parameter as a string or ID? In any case, the relatedTo parameter will receive a string in your code, which might trip up the query. If the parameter is passed as an ID, passing it as an integer instead of a string might be enough:
  
  ```php
  'relatedTo' => (int) $projectCategory,
  
  ```
  
  However, it might be cleaner to get the actual category element based on the ID and pass that to `relatedTo`. This will also allow you to check if the specified category actually exists (and optionally return an HTTP error for invalid parameters).
  
  ```php
  $criteria = ['section' => 'projects'];
  $projectCategory = \Craft::$app->request->getQueryParam('projectCategory');
  // this assumes the category is passed as an ID
  $category = \craft\elements\Category::find()->id((int) $projectCategory)->one();
  if ($category) {
      $criteria['relatedTo'] = $category;
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Perfect, thanks for your reply. This works for me!  
  
  ---
  
  