<p>You'd do it just like querying for any other element in Craft.  i.e.</p>

<pre><code>{% set results = craft.entries({
    search: query,
    section: 'news'
}) %}
</code></pre>

CC BY-SA 3.0
2017-10-26T15:56:11.917Z
Thanks. So where would I add this?  Within the <form> tag?
fodney
CC BY-SA 3.0
2017-10-26T23:26:10.160Z
Like so: https://craftcms.com/docs/templating/search-form
Brad Bell
CC BY-SA 3.0
2017-10-27T00:38:11.093Z
Thanks. Yes, I’ve seen that vMy question was can I limit the channel in the form rather than the results template or will I need two separate results templates, one for news and one for all?

Thanks.
fodney
CC BY-SA 3.0
2017-11-03T15:10:30.810Z
Just wondered if this was possible or not. If not I'll just go ahead and create separate results templates. Thanks!
fodney
CC BY-SA 3.0
2017-10-25T23:44:02.590Z
2017-10-25T23:44:02.590Z
Brad Bell
<p>Totally possible. Easy way to do it, is to add a hidden input field to the form for whatever section you want to search – the input field can be named anything, e.g. <code>s</code>:</p>

<pre><code>&lt;form action="{{ url('search/results') }}"&gt;
    &lt;input type="search" name="q" placeholder="Search"&gt;
    &lt;input type="hidden" name="s" value="vegetables"/&gt;

    &lt;input type="submit" value="Go"&gt;
&lt;/form&gt;
</code></pre>

<p>The above would add an additional GET variable to the search request, named <code>s</code>. Just like with the <code>q</code> parameter, you can use <a href="https://craftcms.com/docs/templating/craft.request" rel="nofollow noreferrer"><code>craft.request</code></a> to pick up that parameter's value in your search results template, and add it to the the <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow noreferrer">ElementCriteriaModel</a> (i.e. <code>craft.entries</code>, in this case):</p>

<pre><code>{% set query = craft.request.getParam('q') %}
{% set section = craft.request.getParam('s')|default('news') %}
{% set entries = craft.entries.section(section).search(query).order('score') %}
...
</code></pre>

<p>Note that it's wise to use the <code>|default</code> filter to add a default value to the <code>section</code> variable, in case the <code>s</code> GET parameter is missing or empty, for some reason – if you don't pass a section handle to the ECM, Craft searches <em>all</em> sections, which is probably not what you want.</p>

<p>Further: If you wanted to search multiple sections, you could do something like this:</p>

<pre><code>&lt;form action="{{ url('search/results') }}"&gt;
    &lt;input type="hidden" name="s[]" value="vegetables"/&gt;
    &lt;input type="hidden" name="s[]" value="fruits"/&gt;
    &lt;input type="hidden" name="s[]" value="berries"/&gt;
    &lt;input type="submit" value="Go"&gt;
&lt;/form&gt;
</code></pre>

<p>...and if you wanted to give the user control over which sections to search, you could opt for an input type other than hidden, e.g. checkboxes:</p>

<pre><code>&lt;input type="checkbox" name="s[]" value="vegetables"/&gt;Vegetables
&lt;input type="checkbox" name="s[]" value="fruits"/&gt;Fruits
&lt;input type="checkbox" name="s[]" value="berries"/&gt;Berries
</code></pre>

<p>And finally, the same strategy would, of course, work fine for any other parameter/field value you can pass to an ElementCriteriaModel. You could, for instance, add a dropdown field enabling the user to filter search results by author:</p>

<pre><code>&lt;form action="{{ url('search/results') }}"&gt;
    &lt;input type="search" name="q" placeholder="Search"&gt;
    &lt;select name="a"&gt;
        &lt;option value=""&gt;All authors&lt;/option&gt;
        &lt;option value="3"&gt;Julia&lt;/option&gt;
        &lt;option value="53"&gt;Gary&lt;/option&gt;
        &lt;option value="13"&gt;Susie&lt;/option&gt;
    &lt;/select&gt;
    &lt;input type="submit" value="Go"&gt;
&lt;/form&gt;
</code></pre>

<p>In this case, it'd probably make sense to default to <code>null</code>, which would give you results for all authors:</p>

<pre><code>{% set query = craft.request.getParam('q') %}
{% set authorId = craft.request.getParam('a')|default(null) %}
{% set entries = craft.entries.authorId(author).search(query).order('score') %}
...
</code></pre>

CC BY-SA 3.0
2017-12-07T18:09:07.397Z
This worked great!  Thanks so much for the detailed explanation.
fodney
CC BY-SA 3.0
2017-12-07T18:22:16.073Z
That's awesome, @fodney – feel free to mark the answer as accepted if it did the trick :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2018-02-15T20:04:32.463Z
one follow up question to this post.  How would you specify that a section not be searched? Sort of like: <input type="hidden" name="s[]" value="not vegetables"/> thx
fodney
CC BY-SA 3.0
2017-11-25T07:06:25.640Z
2017-11-25T07:11:45.950Z
2017-11-25T07:11:45.950Z
Mats Mikkel Rummelhoff
<p>I'd like to use the same results template for all of my search functions so I'm wondering if it's possible to limit the search to a specific channel i.e. news or calendar in the form rather than in the results template.</p>

<p>Thanks!</p>

<search>
Limit search by channel on the form rather than the results template
# Limit search by channel on the form rather than the results template

(Asked by: username_0 on 2017-10-25)

I'd like to use the same results template for all of my search functions so I'm wondering if it's possible to limit the search to a specific channel i.e. news or calendar in the form rather than in the results template.

Thanks!

**Tags:** search

---

## 2 answers

### Answer by username_1 on 2017-10-25. Score: 1 | Accepted answer

You'd do it just like querying for any other element in Craft.  i.e.
{% set results = craft.entries({
    search: query,
    section: 'news'
}) %}

> **Comments:**
> **username_0**: Thanks. So where would I add this?  Within the <form> tag?  
> **username_1**: Like so: https://craftcms.com/docs/templating/search-form  
> **username_0**: Thanks. Yes, I’ve seen that vMy question was can I limit the channel in the form rather than the results template or will I need two separate results templates, one for news and one for all?  Thanks.  
> **username_0**: Just wondered if this was possible or not. If not I'll just go ahead and create separate results templates. Thanks!  

---

### Answer by username_2 on 2017-11-25. Score: 1

Totally possible. Easy way to do it, is to add a hidden input field to the form for whatever section you want to search – the input field can be named anything, e.g. s:

    
    

    


The above would add an additional GET variable to the search request, named s. Just like with the q parameter, you can use craft.request to pick up that parameter's value in your search results template, and add it to the the ElementCriteriaModel (i.e. craft.entries, in this case):
{% set query = craft.request.getParam('q') %}
{% set section = craft.request.getParam('s')|default('news') %}
{% set entries = craft.entries.section(section).search(query).order('score') %}
...

Note that it's wise to use the |default filter to add a default value to the section variable, in case the s GET parameter is missing or empty, for some reason – if you don't pass a section handle to the ECM, Craft searches all sections, which is probably not what you want.
Further: If you wanted to search multiple sections, you could do something like this:

    
    
    
    


...and if you wanted to give the user control over which sections to search, you could opt for an input type other than hidden, e.g. checkboxes:
Vegetables
Fruits
Berries

And finally, the same strategy would, of course, work fine for any other parameter/field value you can pass to an ElementCriteriaModel. You could, for instance, add a dropdown field enabling the user to filter search results by author:

    
    
        All authors
        Julia
        Gary
        Susie
    
    


In this case, it'd probably make sense to default to null, which would give you results for all authors:
{% set query = craft.request.getParam('q') %}
{% set authorId = craft.request.getParam('a')|default(null) %}
{% set entries = craft.entries.authorId(author).search(query).order('score') %}
...

> **Comments:**
> **username_0**: This worked great!  Thanks so much for the detailed explanation.  
> **username_2**: That's awesome, @fodney – feel free to mark the answer as accepted if it did the trick :)  
> **username_0**: one follow up question to this post.  How would you specify that a section not be searched? Sort of like: <input type="hidden" name="s[]" value="not vegetables"/> thx  

---

