<p>I think I just had my Craft "Lightbulb moment".</p>

<p>I've got this working now. Once I realised that I needed to retrieve the category as an object in order to match against it using relatedTo it made more sense.</p>

<p>I also realised that I needed to used a wildcard rather than an empty string if a parameter was missing.</p>

<p>Here's my solution:</p>

<pre><code>{% set cat = craft.request.getParam('category') ? craft.request.getParam('category') : '' %}
{% set start_date = craft.request.getParam('start_date') ? craft.request.getParam('start_date') : 0 %}
{% set wage_from = craft.request.getParam('wage_from') ? craft.request.getParam('wage_from') : 0 %}
{% set wage_to = craft.request.getParam('wage_to') ? craft.request.getParam('wage_to') : 9999 %}
{% set location = craft.request.getParam('location') ? craft.request.getParam('location') : '*' %}

{%
  set category = craft.entries({
        section: 'apprenticeshipsCats',
        title: cat
  })
%}

{%
  set results = craft.entries({
      section: 'vacancies',
      relatedTo: category,
      location: [
            'and',
            '= ' ~ location
        ],
        wage: [
          'and',
          '&gt;= ' ~ wage_from,
          '&lt;= ' ~ wage_to,
        ],
        startDate: [
          'and',
          '&gt;= ' ~ start_date
        ]
    })

%}
</code></pre>

<p>So I still fetch the parameters and set default values for them, note the wildcard if "Location" is empty.</p>

<p>But then I get the category to be searched as a craft entries object.
Once I have that I can use its reference in my results query and bingo!</p>

CC BY-SA 3.0
2015-10-12T12:29:19.880Z
2015-10-12T12:29:19.880Z
foamcow
<p>I'm trying to fetch a set of entries based on search parameters. The search is an "and" search, meaning that if criteria exist then they should all match for there to be a result returned.</p>

<p>I have managed to get this working with all the criteria except for one which needs to match a relationship.</p>

<p>My code below shows what I have so far. The relationship field is named "category" (although this actually relates to an entry in another channel).</p>

<pre><code>{% set cat = craft.request.getParam('category') ? craft.request.getParam('category') : '' %}
{% set start_date = craft.request.getParam('start_date') ? craft.request.getParam('start_date') : 0 %}
{% set wage_from = craft.request.getParam('wage_from') ? craft.request.getParam('wage_from') : 0 %}
{% set wage_to = craft.request.getParam('wage_to') ? craft.request.getParam('wage_to') : 9999 %}
{% set location = craft.request.getParam('location') ? craft.request.getParam('location') : '' %}


{% set results = craft.entries({
    section: 'vacancies',
    order: 'score',
    relatedTo: [
          'and',
          { targetElement: cat },
          { field: 'category' }
      ],
    location: [
          'and',
          '= ' ~ location
      ],
    wage: [
          'and',
          '&gt;= ' ~ wage_from,
          '&lt;= ' ~ wage_to,
      ],
      startDate: [
        'and',
        '&gt;= ' ~ start_date
      ]
  }) %}
</code></pre>

<p>It all seems to work until I add in the relationship. I know I'm not formatting this part of the query correctly but cannot work out how it should be formatted.</p>

<p>Basically, I am searching the channel "vacancies". The "category" field is a relationship with an entry in a channel named "apprenticeshipsCategories".</p>

<p>How do I do this?</p>

CC BY-SA 3.0
2015-10-12T12:24:28.363Z
I think I've cracked this... just testing and will post my solution!
foamcow
<relations><search><elementcriteriamodel>
Searching for entries across multiple fields including a relationship
# Searching for entries across multiple fields including a relationship

(Asked by: username_0 on 2015-10-12)

I'm trying to fetch a set of entries based on search parameters. The search is an "and" search, meaning that if criteria exist then they should all match for there to be a result returned.
I have managed to get this working with all the criteria except for one which needs to match a relationship.
My code below shows what I have so far. The relationship field is named "category" (although this actually relates to an entry in another channel).
{% set cat = craft.request.getParam('category') ? craft.request.getParam('category') : '' %}
{% set start_date = craft.request.getParam('start_date') ? craft.request.getParam('start_date') : 0 %}
{% set wage_from = craft.request.getParam('wage_from') ? craft.request.getParam('wage_from') : 0 %}
{% set wage_to = craft.request.getParam('wage_to') ? craft.request.getParam('wage_to') : 9999 %}
{% set location = craft.request.getParam('location') ? craft.request.getParam('location') : '' %}


{% set results = craft.entries({
    section: 'vacancies',
    order: 'score',
    relatedTo: [
          'and',
          { targetElement: cat },
          { field: 'category' }
      ],
    location: [
          'and',
          '= ' ~ location
      ],
    wage: [
          'and',
          '>= ' ~ wage_from,
          '<= ' ~ wage_to,
      ],
      startDate: [
        'and',
        '>= ' ~ start_date
      ]
  }) %}

It all seems to work until I add in the relationship. I know I'm not formatting this part of the query correctly but cannot work out how it should be formatted.
Basically, I am searching the channel "vacancies". The "category" field is a relationship with an entry in a channel named "apprenticeshipsCategories".
How do I do this?

**Tags:** relations, search, elementcriteriamodel

> **Comments:**
> **username_0**: I think I've cracked this... just testing and will post my solution!  

---

## 1 answers

### Answer by username_0 on 2015-10-12. Score: 2 | Accepted answer

I think I just had my Craft "Lightbulb moment".
I've got this working now. Once I realised that I needed to retrieve the category as an object in order to match against it using relatedTo it made more sense.
I also realised that I needed to used a wildcard rather than an empty string if a parameter was missing.
Here's my solution:
{% set cat = craft.request.getParam('category') ? craft.request.getParam('category') : '' %}
{% set start_date = craft.request.getParam('start_date') ? craft.request.getParam('start_date') : 0 %}
{% set wage_from = craft.request.getParam('wage_from') ? craft.request.getParam('wage_from') : 0 %}
{% set wage_to = craft.request.getParam('wage_to') ? craft.request.getParam('wage_to') : 9999 %}
{% set location = craft.request.getParam('location') ? craft.request.getParam('location') : '*' %}

{%
  set category = craft.entries({
        section: 'apprenticeshipsCats',
        title: cat
  })
%}

{%
  set results = craft.entries({
      section: 'vacancies',
      relatedTo: category,
      location: [
            'and',
            '= ' ~ location
        ],
        wage: [
          'and',
          '>= ' ~ wage_from,
          '<= ' ~ wage_to,
        ],
        startDate: [
          'and',
          '>= ' ~ start_date
        ]
    })

%}

So I still fetch the parameters and set default values for them, note the wildcard if "Location" is empty.
But then I get the category to be searched as a craft entries object.
Once I have that I can use its reference in my results query and bingo!


---

