<p>The <code>relatedTo</code> parameter requires one of the following as an argument:
- AssetFileModel
- CategoryModel
- EntryModel
- UserModel
- TagModel
- An element’s ID
- An array of element objects and/or IDs</p>

<p>Passing in a string from the URL will not work. If you are not on a template that is the result of a category route, you need to fetch the category first.</p>

<pre><code>{% set thisCategory = craft.categories.handle(lastSeg).first() %}
</code></pre>

<p>You can then use <code>thisCategory</code> as your argument in the <code>relatedTo</code> parameter.</p>

<pre><code>{% set meubels = craft.entries.section(‘meubels’).relatedTo(thisCategory) %}
</code></pre>

CC BY-SA 3.0
2015-10-16T07:20:39.820Z
Thanks a lot! I was not aware of the required first line.
GeirrBenayahu
CC BY-SA 3.0
2015-10-16T07:21:58.143Z
Although it first did not work as expected (it returned all entries that had any category connected to it). I changed `{% set thisCategory = craft.categories.handle(lastSeg).first() %}` to `{% set thisCategory = craft.categories.title(lastSeg) %}` and now it works! :)
GeirrBenayahu
CC BY-SA 3.0
2015-10-15T21:56:07.143Z
2015-10-15T21:56:07.143Z
Aaron Berkowitz
<p>It's a pretty basic question, but for some reason all of the possible solutions do not work for me. </p>

<p>I tried this: <a href="https://craftcms.com/docs/relations#the-relatedTo-param" rel="nofollow noreferrer">https://craftcms.com/docs/relations#the-relatedTo-param</a>
And also this (which I really expected to solve my problem): <a href="https://craftcms.com/support/entries-related-to-category" rel="nofollow noreferrer">https://craftcms.com/support/entries-related-to-category</a></p>

<p>I'm pretty sure I may have overlooked something, but I can't see what.</p>

<p>I made this:</p>

<pre><code>{% set lastSeg = craft.request.lastSegment %}
{% set meubels = craft.entries.section('meubels').relatedTo(lastSeg) %}
</code></pre>

<p>And use this as a test:</p>

<pre><code>{% for meub in meubels %}
test&lt;br&gt;
{{ meub.title }}&lt;br&gt;
{% endfor %}
</code></pre>

<p>According to every answer to related questions this should work, but unfortunately it returns nothing. But <code>lastSegment</code> does return the required string from the URL. (At least, I'm assuming this must be a string)</p>

<p>I made a list of categories which all seem to route just fine, no problems there.</p>

CC BY-SA 3.0
2015-10-15T21:52:36.333Z
Are you trying to do this from a url that is a category route?
Aaron Berkowitz
CC BY-SA 3.0
2015-10-16T07:20:17.920Z
Yes, I try to get the last segment from the url and the url is analysed through a category route with a wildcard.
GeirrBenayahu
CC BY-SA 3.0
2015-10-16T12:53:11.740Z
If the URL is routed form your category, you have access to a variable `category` in your template. `category` is the category model for whatever category you were routed to, so you might be able to skip the first step in my answer!
Aaron Berkowitz
CC BY-SA 3.0
2015-10-17T08:36:47.203Z
Ah, indeed, you're right. This saves some lines of code. Thanks once more! :)
GeirrBenayahu
<templating><entries><categories>
How can I use categories to filter entries?
# How can I use categories to filter entries?

(Asked by: username_0 on 2015-10-15)

It's a pretty basic question, but for some reason all of the possible solutions do not work for me. 
I tried this: https://craftcms.com/docs/relations#the-relatedTo-param
And also this (which I really expected to solve my problem): https://craftcms.com/support/entries-related-to-category
I'm pretty sure I may have overlooked something, but I can't see what.
I made this:
{% set lastSeg = craft.request.lastSegment %}
{% set meubels = craft.entries.section('meubels').relatedTo(lastSeg) %}

And use this as a test:
{% for meub in meubels %}
test
{{ meub.title }}
{% endfor %}

According to every answer to related questions this should work, but unfortunately it returns nothing. But lastSegment does return the required string from the URL. (At least, I'm assuming this must be a string)
I made a list of categories which all seem to route just fine, no problems there.

**Tags:** templating, entries, categories

> **Comments:**
> **username_1**: Are you trying to do this from a url that is a category route?  
> **username_0**: Yes, I try to get the last segment from the url and the url is analysed through a category route with a wildcard.  
> **username_1**: If the URL is routed form your category, you have access to a variable `category` in your template. `category` is the category model for whatever category you were routed to, so you might be able to skip the first step in my answer!  
> **username_0**: Ah, indeed, you're right. This saves some lines of code. Thanks once more! :)  

---

## 1 answers

### Answer by username_1 on 2015-10-15. Score: 2 | Accepted answer

The relatedTo parameter requires one of the following as an argument:
- AssetFileModel
- CategoryModel
- EntryModel
- UserModel
- TagModel
- An element’s ID
- An array of element objects and/or IDs
Passing in a string from the URL will not work. If you are not on a template that is the result of a category route, you need to fetch the category first.
{% set thisCategory = craft.categories.handle(lastSeg).first() %}

You can then use thisCategory as your argument in the relatedTo parameter.
{% set meubels = craft.entries.section(‘meubels’).relatedTo(thisCategory) %}

> **Comments:**
> **username_0**: Thanks a lot! I was not aware of the required first line.  
> **username_0**: Although it first did not work as expected (it returned all entries that had any category connected to it). I changed `{% set thisCategory = craft.categories.handle(lastSeg).first() %}` to `{% set thisCategory = craft.categories.title(lastSeg) %}` and now it works! :)  

---

