<p>I'm not sure if you can get multiple POST parameters with one <code>getParam</code> request (maybe it works if you request them as an array?).</p>

<p>What definitely works is to get the parameters separately:</p>

<pre><code>{% set c = craft.request.getParam('c') %}
{% set q = craft.request.getParam('q') %}

{% set query = c ~ ' ' ~ q %}
</code></pre>

<p>.</p>

<p><em>Edit:</em></p>

<pre><code>{# Request the category parameter
 --&gt; see other answer on how to proceed with this #}
{% set catParam = craft.request.getParam('c') %}

{# Request search parameters #}
{% set searchParamQ = craft.request.getParam('q') %}
{% set searchParamR = craft.request.getParam('r') %}
{% set searchParamS = craft.request.getParam('s') %}
{% set searchParamT = craft.request.getParam('t') %}

{# Set `searchParam` before conditionals #}
{% set searchParam = '' %}

{# Add parameters with `OR` logic #}
{% set searchParam = searchParamQ ? searchParam ~ ' OR ' ~ searchParamQ : searchParam %}
{% set searchParam = searchParamR ? searchParam ~ ' OR ' ~ searchParamR : searchParam %}
{% set searchParam = searchParamS ? searchParam ~ ' OR ' ~ searchParamR : searchParam %}
{% set searchParam = searchParamT ? searchParam ~ ' OR ' ~ searchParamR : searchParam %}

{# Beautify `searchParam` #}
{% set searchParam = searchParam|trim|trim('OR ') %}
</code></pre>

CC BY-SA 3.0
2014-09-04T12:33:05.223Z
Thanks for this.

It does work in the sense that if one field is selected it'll display search results. 

But if two or more are entered it doesn't display any. what would really be cool is it listed the parameters with and/or function between them.
Dominic Leonard
CC BY-SA 3.0
2014-09-04T13:05:32.497Z
Hey @dominic, you should really look into that other approach I posted and NOT use the `search()` parameter for you categories! I was close to delete this answer because of that.
carlcs
CC BY-SA 3.0
2014-09-04T13:11:18.493Z
What do you mean with "two or more entered"? Are there other parameters besides `c` and `q` you didn't mention in the question? Yes, you can conditionally add `OR` to the search string, but what types of parameters these are is important, because you should really avoid feeding all of them into `search()`!
carlcs
CC BY-SA 3.0
2014-09-04T10:06:56.803Z
2014-09-04T14:49:36.003Z
2014-09-04T14:49:36.003Z
carlcs
<p>You should process your two POST parameters differently. One is responsible for your <code>search</code> parameter in your results template and the other should go into a <code>relatedTo</code> parameter. That's why I recommend to get them separately (it's probably not even possible to get them with a single <code>getParam</code>, but I'm not sure).</p>

<p>Here's how I'd proceed with the category parameter 'c'. For starters I'd use the category's slug as the value, just to be save.</p>

<p>_search.html:</p>

<pre><code>{% for category in categories %}
    &lt;option value="{{ category.slug }}"&gt;{{ category.title }}&lt;/option&gt;
{% endfor %}
</code></pre>

<p>Then use that slug to grab the right category with a <code>craft.categories</code> ElementCriteriaModel. Use <code>first()</code> to get the first (and only) category model that matches the criteria and use it to compose the relation parameter. The category is the taget element because you select the category from your "News" entry and not the other way round.</p>

<p>_results.html:</p>

<pre><code>{% set searchParam = craft.request.getParam('q') %}
{% set catSlug = craft.request.getParam('c') %}

{% set cat = craft.categories.group('news').slug(catSlug).first %}
{% set relationParam = catSlug ? {targetElement: cat} : '' %}

{% set entries = craft.entries.section('news').relatedTo(relationParam).search(searchParam) %}

{% if entries|length %}

    {% for entry in entries %}
        &lt;p&gt;{{ entry.title }}&lt;/p&gt;
    {% endfor %}

{% else %}

    &lt;p&gt;Your search for “{{ searchParam }}” in “{{ cat.title }}” didn’t return any results.&lt;/p&gt;

{% endif %}
</code></pre>

CC BY-SA 3.0
2015-09-16T18:23:04.883Z
I'm way late to the party but I also found that along with this thread, [this one](http://craftcms.stackexchange.com/questions/6837/building-a-simple-search-by-multiple-categories) was also very helpful.
Damon
CC BY-SA 3.0
2014-09-14T20:34:59.073Z
this is great. what if I want to be able to search in multiple category-groups. how can I have more than one targetElement?
portnull
CC BY-SA 3.0
2014-10-29T16:03:53.160Z
@portnull sorry totally forgot about your question. You do this similar to how you combine criteria in other parameters. There's an extra section in the Craft Docs Article ["The relatedTo Param"](http://buildwithcraft.com/docs/relations#the-relatedTo-param) about this: ["Passing Multiple Relation Criteria"](http://buildwithcraft.com/docs/relations#the-relatedTo-param), because the syntax is a little confusing.
carlcs
CC BY-SA 3.0
2014-09-04T11:15:25.790Z
2014-09-04T15:29:34.357Z
2014-09-04T15:29:34.357Z
carlcs
<p>Not sure of the best way to progress with this but i'm trying to create an advanced search on a Craft site.</p>

<p>What it needs is a select dropdown for Authors, Categories and a normal text input. The search options can be stacked and used singularly.</p>

<p>I'm trying a couple of things  which I'll post here if they work but any assistance would be grand.</p>

<p>Here is the basics to being with.</p>

<pre><code>&lt;form class="search" action="{{ url('news/results') }}"&gt;
    &lt;div class="col col-33"&gt;
        &lt;label&gt;Category&lt;/label&gt;
        &lt;div class="filter"&gt;
            &lt;select name="c"&gt;
            {% set categories = craft.categories.find({ group: 'news' }) %}
                {% for category in categories %}
                &lt;option&gt;{{ category.title }}&lt;/option&gt;
            {% endfor %}
            &lt;/select&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col col-33"&gt;
        &lt;label&gt;Keywords&lt;/label&gt;
        &lt;input type="text" name="q"&gt;
    &lt;/div&gt;
    &lt;div class="col col-33"&gt;
         &lt;button type="submit" class="btn search"&gt;
                Begin Search
         &lt;/button&gt;
    &lt;/div&gt;
&lt;/form&gt;
</code></pre>

<p>And then on the "search results" page for example...</p>

<pre><code>{% set query = craft.request.getParam('q', 'c') %}
    {% set entries = craft.entries.search(query).order('score').section('news').limit(10) %}

        {% if entries|length %}

            {% for entry in entries %}

            {% endfor %}

        {% else %}
            &lt;p&gt;Your search for “{{ query }}” didn’t return any results.&lt;/p&gt;
        {% endif %}
</code></pre>

<p>I know i'm probably missing something extremely obvious here as i'm sure it's fairly simple to achieve.</p>

CC BY-SA 3.0
2014-08-13T19:57:11.770Z
Hey Dominic, an example of any code that you've already tried would be helpful. As it stands right now, this is a pretty broad question, so we can't tell which direction you're currently headed in.
Lindsey D
CC BY-SA 3.0
2014-08-15T14:54:18.117Z
Edited original to house some "code"
Dominic Leonard
<categories><search><entrymodel>
Advanced Search
# Advanced Search

(Asked by: username_0 on 2014-08-13)

Not sure of the best way to progress with this but i'm trying to create an advanced search on a Craft site.
What it needs is a select dropdown for Authors, Categories and a normal text input. The search options can be stacked and used singularly.
I'm trying a couple of things  which I'll post here if they work but any assistance would be grand.
Here is the basics to being with.

    
        Category
        
            
            {% set categories = craft.categories.find({ group: 'news' }) %}
                {% for category in categories %}
                {{ category.title }}
            {% endfor %}
            
        
    
    
        Keywords
        
    
    
         
                Begin Search
         
    


And then on the "search results" page for example...
{% set query = craft.request.getParam('q', 'c') %}
    {% set entries = craft.entries.search(query).order('score').section('news').limit(10) %}

        {% if entries|length %}

            {% for entry in entries %}

            {% endfor %}

        {% else %}
            Your search for “{{ query }}” didn’t return any results.
        {% endif %}

I know i'm probably missing something extremely obvious here as i'm sure it's fairly simple to achieve.

**Tags:** categories, search, entrymodel
> **Comments:**
> **username_1**: Hey Dominic, an example of any code that you've already tried would be helpful. As it stands right now, this is a pretty broad question, so we can't tell which direction you're currently headed in.  
> **username_0**: Edited original to house some "code"  

---

## 2 answers

### Answer by username_2 on 2014-09-04. Score: 3 | Accepted answer

I'm not sure if you can get multiple POST parameters with one getParam request (maybe it works if you request them as an array?).
What definitely works is to get the parameters separately:
{% set c = craft.request.getParam('c') %}
{% set q = craft.request.getParam('q') %}

{% set query = c ~ ' ' ~ q %}

.
Edit:
{# Request the category parameter
 --> see other answer on how to proceed with this #}
{% set catParam = craft.request.getParam('c') %}

{# Request search parameters #}
{% set searchParamQ = craft.request.getParam('q') %}
{% set searchParamR = craft.request.getParam('r') %}
{% set searchParamS = craft.request.getParam('s') %}
{% set searchParamT = craft.request.getParam('t') %}

{# Set `searchParam` before conditionals #}
{% set searchParam = '' %}

{# Add parameters with `OR` logic #}
{% set searchParam = searchParamQ ? searchParam ~ ' OR ' ~ searchParamQ : searchParam %}
{% set searchParam = searchParamR ? searchParam ~ ' OR ' ~ searchParamR : searchParam %}
{% set searchParam = searchParamS ? searchParam ~ ' OR ' ~ searchParamR : searchParam %}
{% set searchParam = searchParamT ? searchParam ~ ' OR ' ~ searchParamR : searchParam %}

{# Beautify `searchParam` #}
{% set searchParam = searchParam|trim|trim('OR ') %}

> **Comments:**
> **username_0**: Thanks for this.  It does work in the sense that if one field is selected it'll display search results.   But if two or more are entered it doesn't display any. what would really be cool is it listed the parameters with and/or function between them.  
> **username_2**: Hey @dominic, you should really look into that other approach I posted and NOT use the `search()` parameter for you categories! I was close to delete this answer because of that.  
> **username_2**: What do you mean with "two or more entered"? Are there other parameters besides `c` and `q` you didn't mention in the question? Yes, you can conditionally add `OR` to the search string, but what types of parameters these are is important, because you should really avoid feeding all of them into `search()`!  

---

### Answer by username_2 on 2014-09-04. Score: 5

You should process your two POST parameters differently. One is responsible for your search parameter in your results template and the other should go into a relatedTo parameter. That's why I recommend to get them separately (it's probably not even possible to get them with a single getParam, but I'm not sure).
Here's how I'd proceed with the category parameter 'c'. For starters I'd use the category's slug as the value, just to be save.
_search.html:
{% for category in categories %}
    {{ category.title }}
{% endfor %}

Then use that slug to grab the right category with a craft.categories ElementCriteriaModel. Use first() to get the first (and only) category model that matches the criteria and use it to compose the relation parameter. The category is the taget element because you select the category from your "News" entry and not the other way round.
_results.html:
{% set searchParam = craft.request.getParam('q') %}
{% set catSlug = craft.request.getParam('c') %}

{% set cat = craft.categories.group('news').slug(catSlug).first %}
{% set relationParam = catSlug ? {targetElement: cat} : '' %}

{% set entries = craft.entries.section('news').relatedTo(relationParam).search(searchParam) %}

{% if entries|length %}

    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}

{% else %}

    Your search for “{{ searchParam }}” in “{{ cat.title }}” didn’t return any results.

{% endif %}

> **Comments:**
> **username_4**: this is great. what if I want to be able to search in multiple category-groups. how can I have more than one targetElement?  
> **username_2**: @portnull sorry totally forgot about your question. You do this similar to how you combine criteria in other parameters. There's an extra section in the Craft Docs Article ["The relatedTo Param"](http://buildwithcraft.com/docs/relations#the-relatedTo-param) about this: ["Passing Multiple Relation Criteria"](http://buildwithcraft.com/docs/relations#the-relatedTo-param), because the syntax is a little confusing.  
> **username_3**: I'm way late to the party but I also found that along with this thread, [this one](http://craftcms.stackexchange.com/questions/6837/building-a-simple-search-by-multiple-categories) was also very helpful.  

---

