Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-26T15:56:11.917Z
Answers.Text: Thanks. So where would I add this?  Within the <form> tag?
Answers.UserDisplayName: fodney
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-26T23:26:10.160Z
Answers.Text: Like so: https://craftcms.com/docs/templating/search-form
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-27T00:38:11.093Z
Answers.Text: 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.
Answers.UserDisplayName: fodney
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-03T15:10:30.810Z
Answers.Text: Just wondered if this was possible or not. If not I'll just go ahead and create separate results templates. Thanks!
Answers.UserDisplayName: fodney
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-25T23:44:02.590Z
Answers.LastActivityDate: 2017-10-25T23:44:02.590Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-07T18:09:07.397Z
Answers.Text: This worked great!  Thanks so much for the detailed explanation.
Answers.UserDisplayName: fodney
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-07T18:22:16.073Z
Answers.Text: That's awesome, @fodney – feel free to mark the answer as accepted if it did the trick :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-15T20:04:32.463Z
Answers.Text: 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
Answers.UserDisplayName: fodney
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-25T07:06:25.640Z
Answers.LastActivityDate: 2017-11-25T07:11:45.950Z
Answers.LastEditDate: 2017-11-25T07:11:45.950Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Tags: <search>
Title: Limit search by channel on the form rather than the results template
ThreadText: # 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`:
  
  ```
  <form action="{{ url('search/results') }}">
      <input type="search" name="q" placeholder="Search">
      <input type="hidden" name="s" value="vegetables"/>
  
      <input type="submit" value="Go">
  </form>
  
  ```
  
  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`](https://craftcms.com/docs/templating/craft.request) to pick up that parameter's value in your search results template, and add it to the the [ElementCriteriaModel](https://craftcms.com/docs/templating/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:
  
  ```
  <form action="{{ url('search/results') }}">
      <input type="hidden" name="s[]" value="vegetables"/>
      <input type="hidden" name="s[]" value="fruits"/>
      <input type="hidden" name="s[]" value="berries"/>
      <input type="submit" value="Go">
  </form>
  
  ```
  
  ...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:
  
  ```
  <input type="checkbox" name="s[]" value="vegetables"/>Vegetables
  <input type="checkbox" name="s[]" value="fruits"/>Fruits
  <input type="checkbox" name="s[]" value="berries"/>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:
  
  ```
  <form action="{{ url('search/results') }}">
      <input type="search" name="q" placeholder="Search">
      <select name="a">
          <option value="">All authors</option>
          <option value="3">Julia</option>
          <option value="53">Gary</option>
          <option value="13">Susie</option>
      </select>
      <input type="submit" value="Go">
  </form>
  
  ```
  
  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, @username_0 – 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  
  
  ---
  
  