0

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 %}
    <h3 class="book-title">{{ entry.title }}</h3>
  {% 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:

<div class="testing">
  <p>
    manually relate entries (limit 2) to age of query string parameter
  </p>

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

  <hr>

  <p>
    manually relate entries (limit 2) to country of query string parameter
  </p>

  {% set countryName = craft.categories.group('country').slug(countrySlug).one() %}
  {% for test in craft.entries.section('books').relatedTo(countryName).limit('2').all() %}
    {{ test.title }} <br>
  {% endfor %}
</div><!-- /.testing -->
  • 1
    I'd start by var_dumping the params variable, are you getting what you expected? – RitterKnight May 19 at 18:44
  • Thank you, that helped and I've got it working now – CreateSean May 19 at 21:25
  • Can you add your solution as an official answer, @CreateSean? – Brad Bell May 20 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.