3

As you can see in the code below I'm currently searching for the keyword but how can I make it so it picks up on entries that have a matching keyword, location & salary or if they don't fill out one of the fields then just search using the remaining fields.

{% set keywordQuery = craft.request.getParam('keyword') %}
{% set locationQuery = craft.request.getParam('location') %}
{% set salaryQuery = craft.categories.getParam('salary') %}

{% set entries = craft.entries.search(keywordQuery).order('score') %}

<form action="{{ url('jobs/list') }}" class="submit-this">
     <ul>
         <li>
             <input type="text" name="keyword" placeholder="Keyword">
         </li>
         <li>
             <input type="text" name="location" placeholder="Town, City, Postcode">
         </li>
         <li class="select">
              <select name="salary">
                   {% for category in craft.categories.group('salary') %}
                         <option value="{{ category.slug }}">{{ category.title }}</option>
                   {% endfor %}
              </select>
         </li>
         <li>
              <input type="submit" name="search" value="Search" class="form-submit">
         </li>
    </ul>
</form>

{% for entry in entries %}

    <div class="entry-section{% if loop.index is even %} grey{% endif %}">
        <div class="content">
            <h3>
                {{ entry.title }}
            </h3>
            {{ entry.jobsExcerpt }}
            <div class="footer">
                <ul>
                    {% for block in entry.location %}
                        <li>
                            <img src="{{ block.icon.first().url }}">
                            <span>
                                {% for category in block.category.limit(1) %}
                                    {{ category.title }}
                                {% endfor %}
                            </span>
                        </li>
                    {% endfor %}    
                    {% for block in entry.salary %}
                        <li>
                            <img src="{{ block.icon.first().url }}">
                            <span>
                                {% for category in block.category.limit(1) %}
                                    {{ category.title }}
                                {% endfor %}
                            </span>
                        </li>
                    {% endfor %}
                    {% for block in entry.keyword %}    
                        <li>
                            <img src="{{ block.icon.first().url }}">
                            <span>
                                {% for category in block.category.limit(1) %}
                                    {{ category.title }}
                                {% endfor %}
                            </span>
                        </li>
                    {% endfor %}    
                </ul>
            </div>
        </div>  
        <div class="side-bar">
            <a href="{{ entry.url }}" class="button">
                Apply
            </a>
        </div>
    </div>

{% endfor %}    

I just can't seem to figure it out, any help would be much appreciated.

Thanks.

3

You'd have to set up an ElementCriteriaModel with parameters adapted to your entry type and the values you're getting from the query string.

I'd recommend to read the official docs on that topic

https://craftcms.com/docs/templating/elementcriteriamodel
https://craftcms.com/docs/templating/craft.entries

After that you shouldn't miss Combined searches and filters using Craft on Webstoemp's blog, a great introduction to setting up search forms and "filtering" the results with the ElementCriteriaModel.

| improve this answer | |
  • Thankyou @carlcs managed to figure it out from the documentation and seeing Webstoemp's example. – Adam Ascroft Mar 29 '16 at 13:50
2

I believe that Craft searches and indexes the contents from ALL custom fields. If that's true, then you only need a single search field. e.g.

https://craftcms.com/search?q=field

Display's results from multiple Sections i.e. Plugin Dev, Documentation and Templating

Would you consider separating your search results, based on whichever entries/sections are returned by the search query.

Hope that helps

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.