<p>For those curious how to make sure that <em>all</em> categories match (<code>and</code> instead of <code>or</code>):</p>

<pre><code>'entries-by-category' =&gt; function() {
    $categoryIds = craft()-&gt;request-&gt;getRequiredQuery('categoryIds');

    return [
        'elementType' =&gt; 'Entry',
        'criteria' =&gt; [
            'relatedTo' =&gt; ['targetElement' =&gt; ['and', 123, 234, 567]
        ],
        // ...
    ];
},
</code></pre>

<p>You can even have more related conditions going on and add the <code>and</code> relationship as well like that:</p>

<pre><code>'entries-by-category' =&gt; function() {
    $categoryIds = craft()-&gt;request-&gt;getRequiredQuery('categoryIds');

    return [
        'elementType' =&gt; 'Entry',
        'criteria' =&gt; [
            'relatedTo' =&gt; [
                'and',
                ['targetElement' =&gt; ['and', 22, 33]],
                ['targetElement' =&gt; ['and', 123, 234]]
            ]
        ],
        // ...
    ];
},
</code></pre>

CC BY-SA 3.0
2016-06-06T13:41:42.903Z
2016-06-06T13:41:42.903Z
medoingthings
<p>You can fetch GET parameters using <a href="http://buildwithcraft.com/classreference/services/HttpRequestService#getQuery-detail">HttpRequestService::getQuery()</a> or <a href="http://buildwithcraft.com/classreference/services/HttpRequestService#getRequiredQuery-detail">getRequiredQuery()</a>:</p>

<pre class="lang-php prettyprint-override"><code>'entries-by-category' =&gt; function() {
    $categoryIds = craft()-&gt;request-&gt;getRequiredQuery('categoryIds');

    return [
        'elementType' =&gt; 'Entry',
        'criteria' =&gt; [
            'relatedTo' =&gt; ['targetElement' =&gt; $categoryIds],
        ],
        // ...
    ];
},
</code></pre>

CC BY-SA 3.0
2015-09-08T19:58:12.383Z
Is there a way to require that the returned entries have all the categories assigned to them? With the code above it returns all entries that have at least one of the specified categories. (Should I be asking this as a new SE question?) Thanks!
Jesse Schutt
CC BY-SA 3.0
2017-02-24T17:36:51.683Z
Because I'm a little slow, can anyone provide an example of a full endpoint config using this method? I'm trying to do something similar but can't figure out how to implement this function.
anguiac7
CC BY-SA 3.0
2017-02-27T19:24:42.120Z
@JesseSchutt (sorry just a little late here :) - yes, just set `'and'` as the first item in the `$categoryIds` array.
Brandon Kelly
CC BY-SA 3.0
2017-02-27T19:26:07.110Z
@anguiac7 that is the full endpoint config – `entries-by-category` is the endpoint’s URI pattern. If you need additional help it will probably be easier if you post as a new question.
Brandon Kelly
CC BY-SA 3.0
2017-02-27T19:27:48.477Z
@BrandonKelly ya know, that makes complete sense now. I made a new question [here](http://craftcms.stackexchange.com/questions/19165/elementapi-get-entries-with-category-that-has-category). I'll give it a shot now that I know that's the full config, though, and close the Q if that solves it.
anguiac7
CC BY-SA 3.0
2015-09-07T18:34:31.633Z
2015-09-07T18:34:31.633Z
Brandon Kelly
<p>How should I go about passing multiple parameters to the Element API? </p>

<p>The scenario is that I would like to display all my categories for a given section in a form, and allow the user to select categories, and send them to the API. I would like to receive back a json object of all the entries that have the categories assigned.</p>

<p>I am aware that I can dynamically catch url segments, but I'm not understanding how I would pass something like a GET parameter as a segment.</p>

<p>Thanks for the help!</p>

<json><plugin-elementapi>
Passing multiple parameters to the Element API
# Passing multiple parameters to the Element API

(Asked by: username_0 on 2015-09-07)

How should I go about passing multiple parameters to the Element API?

The scenario is that I would like to display all my categories for a given section in a form, and allow the user to select categories, and send them to the API. I would like to receive back a json object of all the entries that have the categories assigned.

I am aware that I can dynamically catch url segments, but I'm not understanding how I would pass something like a GET parameter as a segment.

Thanks for the help!

**Tags:** json, plugin-elementapi


---

## 2 answers

### Answer by username_2 on 2015-09-07. Score: 7 | Accepted answer

You can fetch GET parameters using HttpRequestService::getQuery() or getRequiredQuery():
'entries-by-category' => function() {
    $categoryIds = craft()->request->getRequiredQuery('categoryIds');

    return [
        'elementType' => 'Entry',
        'criteria' => [
            'relatedTo' => ['targetElement' => $categoryIds],
        ],
        // ...
    ];
},

> **Comments:**
> **username_0**: Is there a way to require that the returned entries have all the categories assigned to them? With the code above it returns all entries that have at least one of the specified categories. (Should I be asking this as a new SE question?) Thanks!  
> **username_3**: Because I'm a little slow, can anyone provide an example of a full endpoint config using this method? I'm trying to do something similar but can't figure out how to implement this function.  
> **username_2**: @JesseSchutt (sorry just a little late here :) - yes, just set `'and'` as the first item in the `$categoryIds` array.  
> **username_2**: @username_3 that is the full endpoint config – `entries-by-category` is the endpoint’s URI pattern. If you need additional help it will probably be easier if you post as a new question.  
> **username_3**: @BrandonKelly ya know, that makes complete sense now. I made a new question [here](http://craftcms.stackexchange.com/questions/19165/elementapi-get-entries-with-category-that-has-category). I'll give it a shot now that I know that's the full config, though, and close the Q if that solves it.  

---

### Answer by username_1 on 2016-06-06. Score: 1

For those curious how to make sure that all categories match (and instead of or):
'entries-by-category' => function() {
    $categoryIds = craft()->request->getRequiredQuery('categoryIds');

    return [
        'elementType' => 'Entry',
        'criteria' => [
            'relatedTo' => ['targetElement' => ['and', 123, 234, 567]
        ],
        // ...
    ];
},

You can even have more related conditions going on and add the and relationship as well like that:
'entries-by-category' => function() {
    $categoryIds = craft()->request->getRequiredQuery('categoryIds');

    return [
        'elementType' => 'Entry',
        'criteria' => [
            'relatedTo' => [
                'and',
                ['targetElement' => ['and', 22, 33]],
                ['targetElement' => ['and', 123, 234]]
            ]
        ],
        // ...
    ];
},


---

