<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>

CC BY-SA 4.0
2018-08-17T17:29:31.270Z
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.
Robin Schambach
CC BY-SA 4.0
2018-08-17T17:40:35.683Z
I would say Robin's answer would be your best bet since you are adding the search within the entries query.
a-am
CC BY-SA 4.0
2018-08-17T15:56:29.583Z
2018-08-18T18:00:41.127Z
2018-08-18T18:00:41.127Z
a-am
<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>

CC BY-SA 4.0
2018-08-17T17:11:14.303Z
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!
Danielle Frappier
CC BY-SA 4.0
2018-08-17T15:59:28.580Z
2018-08-17T15:59:28.580Z
Robin Schambach
<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>

<search>
Search with string, entry type and category
# 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:

    case studies

    

    ebooks

    

    webinars

    

    whitepapers

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:




 Search the library 


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.
{% for item in filteredEntries %}
    {% if query in item.title %}
        …
    {% endif %}
{% endfor %}

Containment Operator
You could also accomplish the filter by textual query in Javascript: 
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 Robin'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
{% set searchResults = craft.entries
    .search(searchTerm)
    .section('resources')
    .type(resourceType)
    .orderBy('score') 
    .relatedTo(resourceCat)
%}

I would create a dynamic rule
$event->rules[] = [
    'pattern'  => '/resources///',
    'route'    => 'module/search/search-elements',
    'defaults' => [
        'type' => '',
        'category' => '',
        'search' => '',
    ]
];

and search in PHP
public function actionSearchElements(string $type, string $category, string $search)

You can then create a Twig function/filter in order to change those
ebooks

to something like 
ebooks

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!  

---

