<p>You haven't <a href="https://craftcms.com/docs/4.x/element-queries.html#executing-element-queries" rel="nofollow noreferrer">executed</a> your <a href="https://craftcms.com/docs/4.x/element-queries.html" rel="nofollow noreferrer">element query</a>.</p>
<p>Try swapping out:</p>
<pre class="lang-twig prettyprint-override"><code>{% set selectedCategory = entry.newsCategory %}
</code></pre>
<p>for:</p>
<pre class="lang-twig prettyprint-override"><code>{% set selectedCategory = entry.newsCategory.ids() %}
</code></pre>

CC BY-SA 4.0
2023-09-25T20:57:54.340Z
Ok tried that and I get this error : Neither the property "ids" nor one of the methods "ids()", "getids()"/"isids()"/"hasids()" or "__call()" exist and have public access in class "craft\fields\data\MultiOptionsFieldData".
bhackett
CC BY-SA 4.0
2023-09-26T09:03:50.280Z
Sorry, I read your question too fast without paying enough attention! If you're using a multiselect (a questionable decision IMO, as they are a UI fraught with UX problems), then you can't use `relatedTo`, because the values are not _elements_. Only elements are "related to" each other. Instead you'd need to use a simple custom field query: https://craftcms.com/docs/4.x/element-queries.html#querying-with-custom-fields ... However, I'd recommend that you switch to using a proper category selector instead of a plain text multi-select field - it's a lot cleaner and more powerful.
James Smith
CC BY-SA 4.0
2023-09-26T16:10:46.747Z
Yes I think you are right; the custom field query is messy too. I'm going to switch to a category selector
bhackett
CC BY-SA 4.0
2023-09-25T16:14:25.523Z
2023-09-25T16:14:25.523Z
James Smith
<p>I am having an issue outputting news stories that are filtered via a multi-select field, newsCategory is the the multi-select fied handle.</p>
<p>Here is my code</p>
<pre><code>&lt;div class=&quot;search-results search-results--col-three&quot;&gt;
{% set selectedCategory = entry.newsCategory %}
{% set latestNews = craft.entries.section('news_entries')
.relatedTo({
 targetElement: selectedCategory,
 field: 'newsCategory'
  })
 .orderBy('postDate desc')
 .all() %}

{% for news_entry in latestNews %}
    {# Display the filtered news entries as needed. #}
{% endfor %}
</code></pre>

<p>There are no errors with the code but it doesn't output any stories, if I take out</p>
<pre><code> targetElement: selectedCategory,
 field: 'newsCategory'})
</code></pre>
<p>I get all my stories output without the filter, so it looks like the issue is with this part of the code. I am using craft 4.5 and I'm not sure if the change to entification has affected this syntax .Any help is appreciated</p>

<relations><field>
Issue with Multiselect field and related entries
# Issue with Multiselect field and related entries

(Asked by: username_0 on 2023-09-25)

I am having an issue outputting news stories that are filtered via a multi-select field, newsCategory is the the multi-select fied handle.
Here is my code

{% set selectedCategory = entry.newsCategory %}
{% set latestNews = craft.entries.section('news_entries')
.relatedTo({
 targetElement: selectedCategory,
 field: 'newsCategory'
  })
 .orderBy('postDate desc')
 .all() %}

{% for news_entry in latestNews %}
    {# Display the filtered news entries as needed. #}
{% endfor %}

There are no errors with the code but it doesn't output any stories, if I take out
 targetElement: selectedCategory,
 field: 'newsCategory'})

I get all my stories output without the filter, so it looks like the issue is with this part of the code. I am using craft 4.5 and I'm not sure if the change to entification has affected this syntax .Any help is appreciated

**Tags:** relations, field


---

## 1 answers

### Answer by username_1 on 2023-09-25. Score: 2 | Accepted answer

You haven't executed your element query.
Try swapping out:
{% set selectedCategory = entry.newsCategory %}

for:
{% set selectedCategory = entry.newsCategory.ids() %}

> **Comments:**
> **username_0**: Ok tried that and I get this error : Neither the property "ids" nor one of the methods "ids()", "getids()"/"isids()"/"hasids()" or "__call()" exist and have public access in class "craft\fields\data\MultiOptionsFieldData".  
> **username_1**: Sorry, I read your question too fast without paying enough attention! If you're using a multiselect (a questionable decision IMO, as they are a UI fraught with UX problems), then you can't use `relatedTo`, because the values are not _elements_. Only elements are "related to" each other. Instead you'd need to use a simple custom field query: https://craftcms.com/docs/4.x/element-queries.html#querying-with-custom-fields ... However, I'd recommend that you switch to using a proper category selector instead of a plain text multi-select field - it's a lot cleaner and more powerful.  
> **username_0**: Yes I think you are right; the custom field query is messy too. I'm going to switch to a category selector  

---

