Answers.Body: <p>There is a <a href="https://docs.craftcms.com/v3/relations.html#the-relatedto-parameter" rel="nofollow noreferrer">related to</a> parameter you can use</p>
  
  
  
  <pre><code>Entry::find()
    -&gt;section($section)
    -&gt;relatedTo(['targetElement' =&gt; $image-&gt;id, 'field' =&gt; 'galleryImages')
    -&gt;all();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-29T06:03:04.713Z
Answers.LastActivityDate: 2018-08-29T06:03:04.713Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I have some entries with a multi-select field called "gallery". We can choose many images to this field.</p>
  
  <p>I have a given image, how can I query entries whose "gallery" contains this given image? Thx!</p>
  
  <p>I have tried like this with no success:</p>
  
  <pre><code>Entry::find()
    -&gt;section($section)
    -&gt;galleryImages('contains' . $image-&gt;id)
    -&gt;all();
  </code></pre>
  
Tags: <plugin-development><custom-fieldtype><query><elementquery>
Title: How to query entries by field values
ThreadText: # How to query entries by field values
  
  (Asked by: username_0 on 2018-08-29)
  
  I have some entries with a multi-select field called "gallery". We can choose many images to this field.
  
  I have a given image, how can I query entries whose "gallery" contains this given image? Thx!
  
  I have tried like this with no success:
  
  ```
  Entry::find()
    ->section($section)
    ->galleryImages('contains' . $image->id)
    ->all();
  
  ```
  
  **Tags:** plugin-development, custom-fieldtype, query, elementquery
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-08-29. Score: 4 | Accepted answer
  
  There is a [related to](https://docs.craftcms.com/v3/relations.html#the-relatedto-parameter) parameter you can use
  
  ```
  Entry::find()
    ->section($section)
    ->relatedTo(['targetElement' => $image->id, 'field' => 'galleryImages')
    ->all();
  
  ```
  
  
  ---
  
  