<p>Something like this should work:</p>

<pre><code>{% set myStation = getCookie('myStation') %}
{% set relatedStation = craft.entries.id(myStation) %}

{% set params = { section: 'blog', limit: 6, offset: 1, relatedTo: relatedStation } %}

{% if category is defined %}
    {% set params = params|merge({relatedTo: ['and',
        { element: relatedStation },
        { element: category }
    ]}) %}
{% endif %}

{% set entries = craft.entries(params) %}
{% paginate entries as pageInfo, pageEntries %}
</code></pre>

<p>The trick with <code>merge</code> is that it will always <em>replace</em> matching keys that it finds.  So you need to tell it to replace it with the proper <code>and</code> syntax.</p>

CC BY-SA 4.0
2018-05-18T22:10:01.867Z
no offense - but did you "solve" the "object could not be converted to an array" error in this case? A few weeks ago I tried your syntax with Craft 3 `['and', category, query, elementId]` and received an exception. I've solved it by doing `['and', ['element': category], [...]]` there was a question here about the same thing because someone else had the same issue
Robin Schambach
CC BY-SA 4.0
2018-05-18T22:22:31.193Z
Seems like it's not the case... As you can see here https://github.com/craftcms/cms/blob/master/src/elements/db/ElementRelationParamParser.php#L107 the first index will be removed (the "and") and the 2nd index, your `relatedStation` object will be passed to the `subParse` function.. At this point you are trying to merge an array with an object https://github.com/craftcms/cms/blob/master/src/elements/db/ElementRelationParamParser.php#L143 Sry if this is the wrong place for this.. I'll delete it if that's the case
Robin Schambach
CC BY-SA 4.0
2018-05-18T22:30:11.413Z
No offence at all. :) But I think you're right looking at the code.   Will update the answer and maybe even test it this time.
Brad Bell
CC BY-SA 4.0
2018-05-18T21:58:13.643Z
2018-05-18T23:01:08.053Z
2018-05-18T23:01:08.053Z
Brad Bell
<p>I have the following code where I'm pulling entries related to another entry which works fine. However I also want to limit the entries returned on a category page and thus am merging in a relatedTo: category</p>

<p>However on the category page it is returning all entries related to that category, not limited to the category AND the related entry</p>

<pre><code>{% set myStation = getCookie('myStation') %}
{% set relatedStation = craft.entries.id(myStation) %}


{# set pagination/categories #}
{% set params = { section: 'blog', limit: 6, offset: 1, relatedTo: relatedStation } %}
{% if category is defined %}
  {% set params = params | merge({ relatedTo: category }) %}
{% endif %}

{% set entries = craft.entries(params) %}
{% paginate entries as pageInfo, pageEntries %}
</code></pre>

<p>I see the <a href="https://docs.craftcms.com/v3/relations.html#the-relatedto-parameter" rel="nofollow noreferrer">docs includes</a> 'and' (bottom of the page) but I'm not sure how to get that in there using merge. Please advise.</p>

<relatedto><merge>
Merging Multiple Relations as params
# Merging Multiple Relations as params

(Asked by: username_0 on 2018-05-18)

I have the following code where I'm pulling entries related to another entry which works fine. However I also want to limit the entries returned on a category page and thus am merging in a relatedTo: category
However on the category page it is returning all entries related to that category, not limited to the category AND the related entry
{% set myStation = getCookie('myStation') %}
{% set relatedStation = craft.entries.id(myStation) %}


{# set pagination/categories #}
{% set params = { section: 'blog', limit: 6, offset: 1, relatedTo: relatedStation } %}
{% if category is defined %}
  {% set params = params | merge({ relatedTo: category }) %}
{% endif %}

{% set entries = craft.entries(params) %}
{% paginate entries as pageInfo, pageEntries %}

I see the docs includes 'and' (bottom of the page) but I'm not sure how to get that in there using merge. Please advise.

**Tags:** relatedto, merge


---

## 1 answers

### Answer by username_1 on 2018-05-18. Score: 3 | Accepted answer

Something like this should work:
{% set myStation = getCookie('myStation') %}
{% set relatedStation = craft.entries.id(myStation) %}

{% set params = { section: 'blog', limit: 6, offset: 1, relatedTo: relatedStation } %}

{% if category is defined %}
    {% set params = params|merge({relatedTo: ['and',
        { element: relatedStation },
        { element: category }
    ]}) %}
{% endif %}

{% set entries = craft.entries(params) %}
{% paginate entries as pageInfo, pageEntries %}

The trick with merge is that it will always replace matching keys that it finds.  So you need to tell it to replace it with the proper and syntax.

> **Comments:**
> **username_2**: no offense - but did you "solve" the "object could not be converted to an array" error in this case? A few weeks ago I tried your syntax with Craft 3 `['and', category, query, elementId]` and received an exception. I've solved it by doing `['and', ['element': category], [...]]` there was a question here about the same thing because someone else had the same issue  
> **username_2**: Seems like it's not the case... As you can see here https://github.com/craftcms/cms/blob/master/src/elements/db/ElementRelationParamParser.php#L107 the first index will be removed (the "and") and the 2nd index, your `relatedStation` object will be passed to the `subParse` function.. At this point you are trying to merge an array with an object https://github.com/craftcms/cms/blob/master/src/elements/db/ElementRelationParamParser.php#L143 Sry if this is the wrong place for this.. I'll delete it if that's the case  
> **username_1**: No offence at all. :) But I think you're right looking at the code.   Will update the answer and maybe even test it this time.  

---

