<p>I have a search results page set up where there are a number of parameters in the query string used to filter results. I've got 3 out of 5 of these working and am stuck on the final 2.</p>

<p>My full template is below with the two parameters that are nto working correctly marked with <strong>NOT WORKING</strong>. </p>

<pre><code>  {% set params = {
    section: 'books',
    limit: 12,
    orderBy: 'authorLastName'
  } %}

  {# text query working #}
  {# set search values from query string #}
  {% set searchQueryKeywords = craft.app.request.getParam('q') %}
  {% if searchQueryKeywords !="" %}
    {% set params = params | merge({ 
      search:'title:' ~ searchQueryKeywords ~ ' OR bookAuthors' ~ searchQueryKeywords, }) 
    %}
  {% endif %}

  {# 
  // NOT WORKING
  // get related country 
  #}
  {% set countrySlug = craft.app.request.getParam('country') ?? null %} 
  {% if countrySlug != null %}
    {% set searchQueryCountry = craft.categories.group('country').slug(countrySlug).one() %}
  {% endif %}                
  {% if countrySlug is defined and searchQueryCountry is defined %}
    {% set params = params | merge ({
      relatedTo: [
        'and',
        { targetElement: searchQueryCountry }
      ]
    }) %}
  {% endif %}


  {# 
  // NOT WORKING
  // if age is not null then merge related to age #}
  {# get related age rating #}
  {% set ageSlug = craft.app.request.getParam('age') ?? null %}
  {% if ageSlug != null %}
    {% set searchQueryAge = craft.categories.group('recommendedAges').slug(ageSlug).one() %}
  {% endif %}                
  {% if ageSlug is defined and searchQueryAge is defined %}
    {% set params = params | merge ({
      relatedTo: [
        'and',
        { targetElement: searchQueryAge }
      ]
    }) %}
  {% endif %}
  {# end age #}


  {# working if authors is not null then merge related to authors #}
  {# get related author #}
  {% set authorSlug = craft.app.request.getParam('author') ?? null %} 
  {% if authorSlug != null %}
    {% set searchQueryAuthor = craft.entries.section('authors').slug(authorSlug).one() %}
  {% endif %}                
  {% if authorSlug is defined and searchQueryAuthor is defined %}
    {% set params = params | merge ({
      relatedTo: [
        'and',
        { targetElement: searchQueryAuthor }
      ]
    }) %}
  {% endif %}
  {# end authors #}

  {# working language #}
  {# this is a checkbox field #}
  {% set searchQueryLanguage = craft.app.request.getParam('language') ?? "null" %}
  {% set languageSearch = 'bookLanguages:' ~ searchQueryLanguage %}                
  {% if searchQueryLanguage !="null" %}
    {% set params = params | merge({ search: languageSearch }) %}
  {% endif %}

  {# set the entries with all the search/fitering params #}
  {% set entries = craft.entries(params) %}
  {% paginate entries as pageInfo, pageEntries %}

  {% for entry in pageEntries %}
    &lt;h3 class="book-title"&gt;{{ entry.title }}&lt;/h3&gt;
  {% endfor %}
</code></pre>

<p>I cannot figure out why <em>country</em> and <em>age</em> are not working. I even manually created a test div with the relationships from the query string and this section outputs results as expected. However I can't get it to work within my main search results area.</p>

<p>Here is my testing div:</p>

<pre><code>&lt;div class="testing"&gt;
  &lt;p&gt;
    manually relate entries (limit 2) to age of query string parameter
  &lt;/p&gt;

  {% set age = craft.categories.group('recommendedAges').slug(ageSlug).one() %}
  {% for test in craft.entries.section('books').relatedTo(age).limit('2').all() %}
    {{ test.title }} &lt;br&gt;
  {% endfor %}

  &lt;hr&gt;

  &lt;p&gt;
    manually relate entries (limit 2) to country of query string parameter
  &lt;/p&gt;

  {% set countryName = craft.categories.group('country').slug(countrySlug).one() %}
  {% for test in craft.entries.section('books').relatedTo(countryName).limit('2').all() %}
    {{ test.title }} &lt;br&gt;
  {% endfor %}
&lt;/div&gt;&lt;!-- /.testing --&gt;
</code></pre>

CC BY-SA 4.0
2020-05-19T18:44:26.187Z
I'd start by ``var_dump``ing the ``params`` variable, are you getting what you expected?
RitterKnight
CC BY-SA 4.0
2020-05-19T21:25:48.520Z
Thank you, that helped and I've got it working now
CreateSean
CC BY-SA 4.0
2020-05-20T21:00:44.610Z
Can you add your solution as an official answer, @CreateSean?
Brad Bell
<search><relatedto><merge>
Search Results not working as expected
# Search Results not working as expected

(Asked by: username_0 on 2020-05-19)

I have a search results page set up where there are a number of parameters in the query string used to filter results. I've got 3 out of 5 of these working and am stuck on the final 2.
My full template is below with the two parameters that are nto working correctly marked with NOT WORKING. 
  {% set params = {
    section: 'books',
    limit: 12,
    orderBy: 'authorLastName'
  } %}

  {# text query working #}
  {# set search values from query string #}
  {% set searchQueryKeywords = craft.app.request.getParam('q') %}
  {% if searchQueryKeywords !="" %}
    {% set params = params | merge({ 
      search:'title:' ~ searchQueryKeywords ~ ' OR bookAuthors' ~ searchQueryKeywords, }) 
    %}
  {% endif %}

  {# 
  // NOT WORKING
  // get related country 
  #}
  {% set countrySlug = craft.app.request.getParam('country') ?? null %} 
  {% if countrySlug != null %}
    {% set searchQueryCountry = craft.categories.group('country').slug(countrySlug).one() %}
  {% endif %}                
  {% if countrySlug is defined and searchQueryCountry is defined %}
    {% set params = params | merge ({
      relatedTo: [
        'and',
        { targetElement: searchQueryCountry }
      ]
    }) %}
  {% endif %}


  {# 
  // NOT WORKING
  // if age is not null then merge related to age #}
  {# get related age rating #}
  {% set ageSlug = craft.app.request.getParam('age') ?? null %}
  {% if ageSlug != null %}
    {% set searchQueryAge = craft.categories.group('recommendedAges').slug(ageSlug).one() %}
  {% endif %}                
  {% if ageSlug is defined and searchQueryAge is defined %}
    {% set params = params | merge ({
      relatedTo: [
        'and',
        { targetElement: searchQueryAge }
      ]
    }) %}
  {% endif %}
  {# end age #}


  {# working if authors is not null then merge related to authors #}
  {# get related author #}
  {% set authorSlug = craft.app.request.getParam('author') ?? null %} 
  {% if authorSlug != null %}
    {% set searchQueryAuthor = craft.entries.section('authors').slug(authorSlug).one() %}
  {% endif %}                
  {% if authorSlug is defined and searchQueryAuthor is defined %}
    {% set params = params | merge ({
      relatedTo: [
        'and',
        { targetElement: searchQueryAuthor }
      ]
    }) %}
  {% endif %}
  {# end authors #}

  {# working language #}
  {# this is a checkbox field #}
  {% set searchQueryLanguage = craft.app.request.getParam('language') ?? "null" %}
  {% set languageSearch = 'bookLanguages:' ~ searchQueryLanguage %}                
  {% if searchQueryLanguage !="null" %}
    {% set params = params | merge({ search: languageSearch }) %}
  {% endif %}

  {# set the entries with all the search/fitering params #}
  {% set entries = craft.entries(params) %}
  {% paginate entries as pageInfo, pageEntries %}

  {% for entry in pageEntries %}
    {{ entry.title }}
  {% endfor %}

I cannot figure out why country and age are not working. I even manually created a test div with the relationships from the query string and this section outputs results as expected. However I can't get it to work within my main search results area.
Here is my testing div:

  
    manually relate entries (limit 2) to age of query string parameter
  

  {% set age = craft.categories.group('recommendedAges').slug(ageSlug).one() %}
  {% for test in craft.entries.section('books').relatedTo(age).limit('2').all() %}
    {{ test.title }} 
  {% endfor %}

  

  
    manually relate entries (limit 2) to country of query string parameter
  

  {% set countryName = craft.categories.group('country').slug(countrySlug).one() %}
  {% for test in craft.entries.section('books').relatedTo(countryName).limit('2').all() %}
    {{ test.title }} 
  {% endfor %}

**Tags:** search, relatedto, merge
> **Comments:**
> **username_1**: I'd start by ``var_dump``ing the ``params`` variable, are you getting what you expected?  
> **username_0**: Thank you, that helped and I've got it working now  
> **username_2**: Can you add your solution as an official answer, @CreateSean?  

---

## 0 answers

