Answers.Body: <p>I wrote an in-depth article covering this a few years ago which might be useful reading: <a href="https://cld.agency/blog/multi-faceted-filtering-in-craftcms" rel="nofollow noreferrer">https://cld.agency/blog/multi-faceted-filtering-in-craftcms</a></p>
  <p>To more directly answer your question though:</p>
  <p>You can only use <code>relatedTo</code> for fetching/filtering by element <em>relationships</em>. A multiselect field is not a relationship to another element, it's just a value stored against the entry. (Relationship fields are <code>entries</code>, <code>assets</code>, <code>categories</code>, and <code>users</code>).</p>
  <p>In order to filter by a normal field, you can simply use the field handle as a parameter in a  standard element query - e.g.:</p>
  <p><code>craft.entries.textFieldHandle('some value').all()</code></p>
  <p>...or as an array (each value will be treated as having an implicit &quot;OR&quot; between it):
  <code>craft.entries.multiSelectHandle(['optionHandle1', 'optionHandle2']).all()</code></p>
  <p>With this knowledge in hand, it should now become clear that you can combine your <code>relatedTo()</code> method to simultaneously filter by a category relationship and a multiselect field like this:</p>
  <pre class="lang-twig prettyprint-override"><code>{% set entries = craft.entries.yearField(arrayOfYears).relatedTo(category.id).all() %}
  </code></pre>
  <p>(the <code>yearField</code> and <code>relatedTo</code> parameters will be joined with an implicit &quot;AND&quot; between them).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-09-02T10:07:51.503Z
Answers.LastActivityDate: 2024-09-02T10:07:51.503Z
Answers.UserDisplayName: James Smith
Body: <p>I have a &quot;Tasks&quot; section. Each entry in this section has a multiselect &quot;Year&quot; field and a category selection field.
  How do I filter by these fields via relatedTo?
  I'm trying to do this:</p>
  <pre><code>{% set selectedFields = [
      'and',
      {element: category.id, field: 'categoryPrint'},
      {element: [year|integer], field: 'yearField'}
  ] %}
  {{ dump(selectedFields) }}
  {% do prints.relatedTo(selectedFields).all() %}
  </code></pre>
  <p>I suspect that the problem is in the yearField field:</p>
  <pre><code>{element: [year|integer], field: 'yearField'}
  </code></pre>
  <p>The 'yearField' field is a multiselect. Each menu item does not have its own id and perhaps because of this I cannot filter the items. Can you tell me what to do?</p>
  
Tags: <craft3><entries><craft4><relatedto>
Title: How to filter elements by multiselect field via relatedTo
ThreadText: # How to filter elements by multiselect field via relatedTo
  
  (Asked by: username_0 on 2024-09-01)
  
  I have a "Tasks" section. Each entry in this section has a multiselect "Year" field and a category selection field.
  How do I filter by these fields via relatedTo?
  I'm trying to do this:
  
  ```
  {% set selectedFields = [
      'and',
      {element: category.id, field: 'categoryPrint'},
      {element: [year|integer], field: 'yearField'}
  ] %}
  {{ dump(selectedFields) }}
  {% do prints.relatedTo(selectedFields).all() %}
  
  ```
  
  I suspect that the problem is in the yearField field:
  
  ```
  {element: [year|integer], field: 'yearField'}
  
  ```
  
  The 'yearField' field is a multiselect. Each menu item does not have its own id and perhaps because of this I cannot filter the items. Can you tell me what to do?
  
  **Tags:** craft3, entries, craft4, relatedto
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-09-02. Score: 1 | Accepted answer
  
  I wrote an in-depth article covering this a few years ago which might be useful reading: <https://cld.agency/blog/multi-faceted-filtering-in-craftcms>
  
  To more directly answer your question though:
  
  You can only use `relatedTo` for fetching/filtering by element *relationships*. A multiselect field is not a relationship to another element, it's just a value stored against the entry. (Relationship fields are `entries`, `assets`, `categories`, and `users`).
  
  In order to filter by a normal field, you can simply use the field handle as a parameter in a standard element query - e.g.:
  
  `craft.entries.textFieldHandle('some value').all()`
  
  ...or as an array (each value will be treated as having an implicit "OR" between it):
  `craft.entries.multiSelectHandle(['optionHandle1', 'optionHandle2']).all()`
  
  With this knowledge in hand, it should now become clear that you can combine your `relatedTo()` method to simultaneously filter by a category relationship and a multiselect field like this:
  
  ```twig
  {% set entries = craft.entries.yearField(arrayOfYears).relatedTo(category.id).all() %}
  
  ```
  
  (the `yearField` and `relatedTo` parameters will be joined with an implicit "AND" between them).
  
  
  ---
  
  