Trying to build params to be able find entries related to an entry and that are related to a specific category I want to generate a list of  events that are related to a specific festival and that are related to a specific category. This goal sits alongside some others that are inspiring me to attempt this in a single template and to do so via building params that are then passed to Craft entries tag. Using Craft 3.

First the base params - these return a fine result.

{# Set base criteria for event query #}
{% set params = {
    section : 'events',
    orderBy : 'eventStartDateTime asc',
    relatedTo: [
        { element: entry.id }
    ],
    limit: null
} %}


Next here are the params to try and get the events associated with a specific festival entry and that are related to a specific category.

{# Segment 4 is about categories #}
{% if craft.app.request.getSegment(4) %}
    {# segment 4 is the category slug #}
    {% set cat = craft.app.request.getSegment(4) %}
    {% set relationParam = {
        relatedTo: ['and',
            { targetElement: cat },
            { element: entry.id }
        ]
    }%}  

    {% set params = params|merge(relationParam) %}
{% endif %} 


What I am seeing in this second scenario is I am not getting results filtered by the category. Feel like I am close but missing something, likely fundamental.
I think the answer you’re looking for is here.

TL;DR:


  relatedTo method accepts a CategoryModel, not the title(slug) of a category.