Answers.Body: <p>Without completely changing your direction and following in Hubspot's footsteps.  From the search box set the query param for the page <code>?q=search-text</code> then as the page refreshes grab the query <code>{% set query = craft.request.getParam('q') %}</code>.  Then loop over your filteredEntries results only showing those entries who's title has the query.</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% for item in filteredEntries %}
      {% if query in item.title %}
          …
      {% endif %}
  {% endfor %}
  </code></pre>
  
  <p><a href="https://twig.symfony.com/doc/2.x/templates.html#containment-operator" rel="nofollow noreferrer">Containment Operator</a></p>
  
  <p>You could also accomplish the filter by textual query in Javascript: 
  <a href="https://stackoverflow.com/questions/27096548/filter-by-search-using-text-found-in-element-within-each-div">Filter by search using text found in element within each div</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-17T17:29:31.270Z
Answers.Text: Keep in mind this won't order them by score and it will only search the title. So chances are high your first results are no actual matches as one would expect but titles that randomly contain the characters. In fact I don't see any reason not to include the search param to the query.
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-17T17:40:35.683Z
Answers.Text: I would say Robin's answer would be your best bet since you are adding the search within the entries query.
Answers.UserDisplayName: a-am
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-17T15:56:29.583Z
Answers.LastActivityDate: 2018-08-18T18:00:41.127Z
Answers.LastEditDate: 2018-08-18T18:00:41.127Z
Answers.UserDisplayName: a-am
Answers.Body: <p>A more elegant way to handle it would be to use PHP, rely on a more complex routing system with default values, don't rely on segments but on routing parameters. Use real URLs instead of relative paths and build your URLs dynamically instead of just inserting the string hard coded in order to be able to filter by multiple values without loosing the already set filters.</p>
  
  <p>However - to include your parameters all you need to do</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set searchResults = craft.entries
      .search(searchTerm)
      .section('resources')
      .type(resourceType)
      .orderBy('score') 
      .relatedTo(resourceCat)
  %}
  </code></pre>
  
  <p>I would create a dynamic rule</p>
  
  <pre class="lang-php prettyprint-override"><code>$event-&gt;rules[] = [
      'pattern'  =&gt; '/resources/&lt;type:\S+&gt;/&lt;category:\S+&gt;/&lt;search:[a-zA-Z0-9-]+&gt;',
      'route'    =&gt; 'module/search/search-elements',
      'defaults' =&gt; [
          'type' =&gt; '',
          'category' =&gt; '',
          'search' =&gt; '',
      ]
  ];
  </code></pre>
  
  <p>and search in PHP</p>
  
  <pre class="lang-php prettyprint-override"><code>public function actionSearchElements(string $type, string $category, string $search)
  </code></pre>
  
  <p>You can then create a Twig function/filter in order to change those</p>
  
  <pre><code>&lt;a href="/resources/ebooks/{{category.slug}}"&gt;ebooks&lt;/a&gt;
  </code></pre>
  
  <p>to something like </p>
  
  <pre><code>&lt;a href="{{ getSearchUrl({category: category.slug}) }}"&gt;ebooks&lt;/a&gt;
  </code></pre>
  
  <p>And build your URL based on your route params + overwrite the new category. It will make it more re-usable and easier to change in the future if you would like to add an additional parameter or need to change the order you only need to change one place instead of many</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-17T17:11:14.303Z
Answers.Text: Thank you so much for alway thoughtful answers. You have helped me out so much with this website build. I really appreciate all your help!
Answers.UserDisplayName: Danielle Frappier
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-17T15:59:28.580Z
Answers.LastActivityDate: 2018-08-17T15:59:28.580Z
Answers.UserDisplayName: Robin Schambach
Body: <p>So I have been asked to build a resource center just like hubspot's. (insert rolleyes here)</p>
  
  <p><a href="https://www.hubspot.com/resources" rel="nofollow noreferrer">https://www.hubspot.com/resources</a></p>
  
  <p>You can click into a type, and then click at topic and it creates a url like so:</p>
  
  <p><a href="https://www.hubspot.com/resources/ebook/advertising" rel="nofollow noreferrer">https://www.hubspot.com/resources/ebook/advertising</a></p>
  
  <p>You can also click a topic and then click type and it will do the same.</p>
  
  <p>I have this part pretty much working. In my category template I have done this: </p>
  
  <pre><code>    Filter by type:
  
      &lt;a href="/resources/case-studies/{{category.slug}}"&gt;case studies&lt;/a&gt;
  
      &lt;br/&gt;&lt;br/&gt;
  
      &lt;a href="/resources/ebooks/{{category.slug}}"&gt;ebooks&lt;/a&gt;
  
      &lt;br/&gt;&lt;br/&gt;
  
      &lt;a href="/resources/webinars/{{category.slug}}}"&gt;webinars&lt;/a&gt;
  
      &lt;br/&gt;&lt;br/&gt;
  
      &lt;a href="/resources/whitepapers/{{category.slug}}"&gt;whitepapers&lt;/a&gt;
  </code></pre>
  
  <p>then I have a routes for each type setup like this: </p>
  
  <p>if the URI looks like this: </p>
  
  <p>/resources/case-studies/*</p>
  
  <p>Loads this template: </p>
  
  <p>resources/_filtered.html </p>
  
  <p>and in that template filtered I do the filtering like this: </p>
  
  <pre><code>        {% set resourceType = craft.request.segment(2) %}
  
          {% set resourceCatSlug = craft.request.segment(3) %}
  
          {% set resourceCat = craft.categories.slug(resourceCatSlug).first() %}
  
          {{resourceType}}
  
          {{resourceCat}}
  
          {% set filteredEntries = craft.entries({
              section:    ['resources'],
              orderBy:    {postDate: SORT_DESC},
              type:       resourceType,
              relatedTo:  resourceCat,
          }).all() %}
  </code></pre>
  
  <p>This all works and I planned to do the same method on the resources types to get to the filtered template. </p>
  
  <p>But where I'm stuck is it was also requested that you could filter your search even further with a string search. </p>
  
  <p>So from this:</p>
  
  <pre><code>&lt;form class="control txt-center" action="{{ url('resources/search/results-filtered') }}"&gt;
  
  &lt;input class="input margin-auto-left-right display-block is-narrow-field " type="search" name="q" placeholder="Search"&gt;
  
  &lt;button class="margin-top-1 button is-info" role="button" type="submit"&gt; Search the library &lt;/button&gt;
  </code></pre>
  
  <p></p>
  
  <p>How can I pass entry type and category to my filtered results?</p>
  
  <pre><code>{% set searchTerm = craft.app.request.getParam('q') %}
  {% set searchResults = craft.entries.search(searchTerm).section('resources').orderBy('score') %}
  </code></pre>
  
  <p>PS, If anyone has a more elegant way to handle this I'm all ears! </p>
  
Tags: <search>
Title: Search with string, entry type and category
ThreadText: # Search with string, entry type and category
  
  (Asked by: username_0 on 2018-08-17)
  
  So I have been asked to build a resource center just like hubspot's. (insert rolleyes here)
  
  <https://www.hubspot.com/resources>
  
  You can click into a type, and then click at topic and it creates a url like so:
  
  <https://www.hubspot.com/resources/ebook/advertising>
  
  You can also click a topic and then click type and it will do the same.
  
  I have this part pretty much working. In my category template I have done this:
  
  ```
      Filter by type:
  
      <a href="/resources/case-studies/{{category.slug}}">case studies</a>
  
      <br/><br/>
  
      <a href="/resources/ebooks/{{category.slug}}">ebooks</a>
  
      <br/><br/>
  
      <a href="/resources/webinars/{{category.slug}}}">webinars</a>
  
      <br/><br/>
  
      <a href="/resources/whitepapers/{{category.slug}}">whitepapers</a>
  
  ```
  
  then I have a routes for each type setup like this:
  
  if the URI looks like this:
  
  /resources/case-studies/\*
  
  Loads this template:
  
  resources/\_filtered.html
  
  and in that template filtered I do the filtering like this:
  
  ```
          {% set resourceType = craft.request.segment(2) %}
  
          {% set resourceCatSlug = craft.request.segment(3) %}
  
          {% set resourceCat = craft.categories.slug(resourceCatSlug).first() %}
  
          {{resourceType}}
  
          {{resourceCat}}
  
          {% set filteredEntries = craft.entries({
              section:    ['resources'],
              orderBy:    {postDate: SORT_DESC},
              type:       resourceType,
              relatedTo:  resourceCat,
          }).all() %}
  
  ```
  
  This all works and I planned to do the same method on the resources types to get to the filtered template.
  
  But where I'm stuck is it was also requested that you could filter your search even further with a string search.
  
  So from this:
  
  ```
  <form class="control txt-center" action="{{ url('resources/search/results-filtered') }}">
  
  <input class="input margin-auto-left-right display-block is-narrow-field " type="search" name="q" placeholder="Search">
  
  <button class="margin-top-1 button is-info" role="button" type="submit"> Search the library </button>
  
  ```
  
  How can I pass entry type and category to my filtered results?
  
  ```
  {% set searchTerm = craft.app.request.getParam('q') %}
  {% set searchResults = craft.entries.search(searchTerm).section('resources').orderBy('score') %}
  
  ```
  
  PS, If anyone has a more elegant way to handle this I'm all ears!
  
  **Tags:** search
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-08-17. Score: 2 | Accepted answer
  
  Without completely changing your direction and following in Hubspot's footsteps. From the search box set the query param for the page `?q=search-text` then as the page refreshes grab the query `{% set query = craft.request.getParam('q') %}`. Then loop over your filteredEntries results only showing those entries who's title has the query.
  
  ```twig
  {% for item in filteredEntries %}
      {% if query in item.title %}
          …
      {% endif %}
  {% endfor %}
  
  ```
  
  [Containment Operator](https://twig.symfony.com/doc/2.x/templates.html#containment-operator)
  
  You could also accomplish the filter by textual query in Javascript:
  [Filter by search using text found in element within each div](https://stackoverflow.com/questions/27096548/filter-by-search-using-text-found-in-element-within-each-div)
  
  > **Comments:**
  > **username_2**: Keep in mind this won't order them by score and it will only search the title. So chances are high your first results are no actual matches as one would expect but titles that randomly contain the characters. In fact I don't see any reason not to include the search param to the query.  
  > **username_1**: I would say username_2's answer would be your best bet since you are adding the search within the entries query.  
  
  ---
  
  ### Answer by username_2 on 2018-08-17. Score: 3
  
  A more elegant way to handle it would be to use PHP, rely on a more complex routing system with default values, don't rely on segments but on routing parameters. Use real URLs instead of relative paths and build your URLs dynamically instead of just inserting the string hard coded in order to be able to filter by multiple values without loosing the already set filters.
  
  However - to include your parameters all you need to do
  
  ```twig
  {% set searchResults = craft.entries
      .search(searchTerm)
      .section('resources')
      .type(resourceType)
      .orderBy('score') 
      .relatedTo(resourceCat)
  %}
  
  ```
  
  I would create a dynamic rule
  
  ```php
  $event->rules[] = [
      'pattern'  => '/resources/<type:\S+>/<category:\S+>/<search:[a-zA-Z0-9-]+>',
      'route'    => 'module/search/search-elements',
      'defaults' => [
          'type' => '',
          'category' => '',
          'search' => '',
      ]
  ];
  
  ```
  
  and search in PHP
  
  ```php
  public function actionSearchElements(string $type, string $category, string $search)
  
  ```
  
  You can then create a Twig function/filter in order to change those
  
  ```
  <a href="/resources/ebooks/{{category.slug}}">ebooks</a>
  
  ```
  
  to something like
  
  ```
  <a href="{{ getSearchUrl({category: category.slug}) }}">ebooks</a>
  
  ```
  
  And build your URL based on your route params + overwrite the new category. It will make it more re-usable and easier to change in the future if you would like to add an additional parameter or need to change the order you only need to change one place instead of many
  
  > **Comments:**
  > **username_0**: Thank you so much for alway thoughtful answers. You have helped me out so much with this website build. I really appreciate all your help!  
  
  ---
  
  