Answers.Body: <p>When you wrap your term in asterisks, e.g. <code>*news*</code>, the generated SQL for this query parameter uses a <code>LIKE</code> operator for the <code>ANDWHERE</code> condition is uses (see <a href="https://docs.craftcms.com/v3/element-queries.html#param-value-syntax" rel="nofollow noreferrer">Element Queries</a>).</p>
  
  <p>Make sure that the values for the Select field don’t overlap (<code>sportnews</code> and <code>news</code> both matching <code>*news*</code>).</p>
  
  <pre><code>'bannerPageCategories' =&gt; "*{$target}*"
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-22T19:09:34.813Z
Answers.Text: Thanks @carics, much appreciated. I am still a noob with craft. Based on the combination of your answer and Robins answer I ended up using : 'bannerPageCategories' => ['=',  "*{$target}*"],.
Answers.UserDisplayName: Ray
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-22T13:47:01.460Z
Answers.LastActivityDate: 2018-05-22T14:07:15.703Z
Answers.LastEditDate: 2018-05-22T14:07:15.703Z
Answers.UserDisplayName: carlcs
Answers.Body: <p>The value is stored as a serialized array in your database, in your case your would have to filter for the <strong>string</strong> <code>"["adsl","fibre"]"</code> (the order is the same like you specified in the field)</p>
  
  <p>You can as well insert an array </p>
  
  <pre><code>['and',  '*adsl*', '*fibre*']
  </code></pre>
  
  <p>or</p>
  
  <pre><code>['or',  '*adsl*', '*fibre*']
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-22T19:10:32.353Z
Answers.Text: Thanks Robin. I ended up going with 'bannerPageCategories' => ['=',  "*{$target}*"], which seemed to do the trick.
Answers.UserDisplayName: Ray
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-22T13:38:42.323Z
Answers.LastActivityDate: 2018-05-22T13:38:42.323Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I am looking for some assistance with the Element API plugin. I need to filter on the <code>bannerPageCategories</code> field which is a multi select checkbox field. The value will be passed via a querystring value. I can filter on a field with a single value but how do I do this for fields which has an array of values.</p>
  
  <p>Any suggestions will be greatly appreciated.</p>
  
  <p>Sample code and json response is below. </p>
  
  <p>Json Response:</p>
  
  <pre><code>    {
        title:"Test Banner 4",
        url:"http://localhost:8888/banners/test-banner-4",
        jsonUrl:"http://localhost:8888/api/banners/17",
        content:"Test.", 
         page-categories:[
         {
           label:"ADSL",
           value:"adsl",
           selected:true
         },
         {
          label:"Fibre",
          value:"fibre",
          selected:true
         }
      ],
     campaignId:"sHKJJK",
     targetUrl:"http://xyx.xaws.co.za",
     image1:"//xyx.xaws.co.za/dev/images/banners/banner-lg.png",
     image2:"//xyx.xaws.co.za/dev/images/banners/banner-md.png",
     image3:"//xyx.xaws.co.za/dev/images/banners/banner-sm.png",
     date_published:"2018-05-22T14:06:05+02:00",
     date_modified:"2018-05-22T14:06:05+02:00"
    }
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>    'api/banners' =&gt; function () {
          \Craft::$app-&gt;response-&gt;headers-&gt;set('Access-Control-Allow-Origin', '*');
          $target = \Craft::$app-&gt;request-&gt;getQueryParam('target');
          $campaign = \Craft::$app-&gt;request-&gt;getQueryParam('campaign');
          return [
              'elementType' =&gt; Entry::class,
              'criteria' =&gt; [
                  'section' =&gt; 'banners',
                  'bannerPageCategories' =&gt; $target,
                  'campaignId' =&gt; $campaign              
              ],
              'transformer' =&gt; function (Entry $entry) {
                  $image1 = $entry-&gt;image1-&gt;first();
                  $image2 = $entry-&gt;image2-&gt;first();
                  $image3 = $entry-&gt;image3-&gt;first();
  
                  return [
                      'title' =&gt; $entry-&gt;title,
                      'url' =&gt; $entry-&gt;url,
                      'jsonUrl' =&gt; UrlHelper::url("api/banners/{$entry-&gt;id}"),
                      'content' =&gt; $entry-&gt;htmlcontent,
                      'page-categories' =&gt; $entry-&gt;bannerPageCategories,
                      'campaignId' =&gt; $entry-&gt;campaignId,
                      'targetUrl' =&gt; $entry-&gt;targetUrl,
                      'image1' =&gt; $image1 ? $image1-&gt;url : null,
                      'image2' =&gt; $image2 ? $image2-&gt;url : null,
                      'image3' =&gt; $image3 ? $image3-&gt;url : null,
                      'date_published' =&gt; $entry-&gt;postDate-&gt;format(\DateTime::ATOM),
                      'date_modified' =&gt; $entry-&gt;dateUpdated-&gt;format(\DateTime::ATOM),
                  ];
              },
          ];
      },
  </code></pre>
  
  <p>Many thanks!</p>
  
Tags: <plugin-elementapi>
Title: Element API - Filter on a value from a multi value field
ThreadText: # Element API - Filter on a value from a multi value field
  
  (Asked by: username_0 on 2018-05-22)
  
  I am looking for some assistance with the Element API plugin. I need to filter on the `bannerPageCategories` field which is a multi select checkbox field. The value will be passed via a querystring value. I can filter on a field with a single value but how do I do this for fields which has an array of values.
  
  Any suggestions will be greatly appreciated.
  
  Sample code and json response is below.
  
  Json Response:
  
  ```
      {
        title:"Test Banner 4",
        url:"http://localhost:8888/banners/test-banner-4",
        jsonUrl:"http://localhost:8888/api/banners/17",
        content:"Test.", 
         page-categories:[
         {
           label:"ADSL",
           value:"adsl",
           selected:true
         },
         {
          label:"Fibre",
          value:"fibre",
          selected:true
         }
      ],
     campaignId:"sHKJJK",
     targetUrl:"http://xyx.xaws.co.za",
     image1:"//xyx.xaws.co.za/dev/images/banners/banner-lg.png",
     image2:"//xyx.xaws.co.za/dev/images/banners/banner-md.png",
     image3:"//xyx.xaws.co.za/dev/images/banners/banner-sm.png",
     date_published:"2018-05-22T14:06:05+02:00",
     date_modified:"2018-05-22T14:06:05+02:00"
    }
  
  ```
  
  Code:
  
  ```
      'api/banners' => function () {
          \Craft::$app->response->headers->set('Access-Control-Allow-Origin', '*');
          $target = \Craft::$app->request->getQueryParam('target');
          $campaign = \Craft::$app->request->getQueryParam('campaign');
          return [
              'elementType' => Entry::class,
              'criteria' => [
                  'section' => 'banners',
                  'bannerPageCategories' => $target,
                  'campaignId' => $campaign              
              ],
              'transformer' => function (Entry $entry) {
                  $image1 = $entry->image1->first();
                  $image2 = $entry->image2->first();
                  $image3 = $entry->image3->first();
  
                  return [
                      'title' => $entry->title,
                      'url' => $entry->url,
                      'jsonUrl' => UrlHelper::url("api/banners/{$entry->id}"),
                      'content' => $entry->htmlcontent,
                      'page-categories' => $entry->bannerPageCategories,
                      'campaignId' => $entry->campaignId,
                      'targetUrl' => $entry->targetUrl,
                      'image1' => $image1 ? $image1->url : null,
                      'image2' => $image2 ? $image2->url : null,
                      'image3' => $image3 ? $image3->url : null,
                      'date_published' => $entry->postDate->format(\DateTime::ATOM),
                      'date_modified' => $entry->dateUpdated->format(\DateTime::ATOM),
                  ];
              },
          ];
      },
  
  ```
  
  Many thanks!
  
  **Tags:** plugin-elementapi
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-05-22. Score: 2 | Accepted answer
  
  When you wrap your term in asterisks, e.g. `*news*`, the generated SQL for this query parameter uses a `LIKE` operator for the `ANDWHERE` condition is uses (see [Element Queries](https://docs.craftcms.com/v3/element-queries.html#param-value-syntax)).
  
  Make sure that the values for the Select field don’t overlap (`sportnews` and `news` both matching `*news*`).
  
  ```
  'bannerPageCategories' => "*{$target}*"
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks @carics, much appreciated. I am still a noob with craft. Based on the combination of your answer and Robins answer I ended up using : 'bannerPageCategories' => ['=',  "*{$target}*"],.  
  
  ---
  
  ### Answer by username_2 on 2018-05-22. Score: 1
  
  The value is stored as a serialized array in your database, in your case your would have to filter for the **string** `"["adsl","fibre"]"` (the order is the same like you specified in the field)
  
  You can as well insert an array
  
  ```
  ['and',  '*adsl*', '*fibre*']
  
  ```
  
  or
  
  ```
  ['or',  '*adsl*', '*fibre*']
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks username_2. I ended up going with 'bannerPageCategories' => ['=',  "*{$target}*"], which seemed to do the trick.  
  
  ---
  
  