<p>It does not work because the function does not accept slugs. Not sure what to answer here. It only accepts objects of type <code>Element</code> and ids as identifier.</p>

<p>You can see the magic here</p>

<pre><code>foreach ($elements as $element) {
if (is_numeric($element)) {
    $relElementIds[] = $element;
} else if ($element instanceof ElementInterface) {
    $relElementIds[] = $element-&gt;id;
} else if ($element instanceof ElementQueryInterface) {
    foreach ($element-&gt;ids() as $id) {
        $relElementIds[] = $id;
    }
}
</code></pre>

<p>These are all kind of things that the <code>relatedTo</code> can use/work with. If you want to get a relation by slug you have to fetch the element first.</p>

<pre><code>.relatedTo(craft.categories.slug('municipal').one())
</code></pre>

CC BY-SA 3.0
2018-04-02T20:51:25.767Z
@terryupton Assuming you're in a category template, you could also just do `.relatedTo(category)` as well.
jsun
CC BY-SA 3.0
2018-04-02T19:57:18.823Z
2018-04-02T20:32:32.703Z
2018-04-02T20:32:32.703Z
Robin Schambach
<p>Can someone tell me why I am going crazy and cannot figure this simple relatedTo issue out?</p>

<p>So I have a categories section called Case Studies and several categories within. I am trying to filter the results to show all entries within a specific category.</p>

<pre><code>{% set entries = craft.entries.section('caseStudies').relatedTo('municipal').find() %}
</code></pre>

<p>won't show any results but using the ID will.</p>

<pre><code>{% set entries = craft.entries.section('caseStudies').relatedTo(422).find() %}
</code></pre>

<p>I've tried all sorts of slugs and nothing is showing. Why can I not figure this out. someone please point out my obvious mistake...</p>

<p>My entry field is called 'caseStudyCategory'</p>

<categories><category><relatedto>
cannot get relatedTo by slug only ID
# cannot get relatedTo by slug only ID

(Asked by: username_0 on 2018-04-02)

Can someone tell me why I am going crazy and cannot figure this simple relatedTo issue out?
So I have a categories section called Case Studies and several categories within. I am trying to filter the results to show all entries within a specific category.
{% set entries = craft.entries.section('caseStudies').relatedTo('municipal').find() %}

won't show any results but using the ID will.
{% set entries = craft.entries.section('caseStudies').relatedTo(422).find() %}

I've tried all sorts of slugs and nothing is showing. Why can I not figure this out. someone please point out my obvious mistake...
My entry field is called 'caseStudyCategory'

**Tags:** categories, category, relatedto


---

## 1 answers

### Answer by username_1 on 2018-04-02. Score: 4 | Accepted answer

It does not work because the function does not accept slugs. Not sure what to answer here. It only accepts objects of type Element and ids as identifier.
You can see the magic here
foreach ($elements as $element) {
if (is_numeric($element)) {
    $relElementIds[] = $element;
} else if ($element instanceof ElementInterface) {
    $relElementIds[] = $element->id;
} else if ($element instanceof ElementQueryInterface) {
    foreach ($element->ids() as $id) {
        $relElementIds[] = $id;
    }
}

These are all kind of things that the relatedTo can use/work with. If you want to get a relation by slug you have to fetch the element first.
.relatedTo(craft.categories.slug('municipal').one())

> **Comments:**
> **username_2**: @terryupton Assuming you're in a category template, you could also just do `.relatedTo(category)` as well.  

---

