Searching for entries across multiple fields including a relationship 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',
          '&gt;= ' ~ wage_from,
          '&lt;= ' ~ wage_to,
      ],
      startDate: [
        'and',
        '&gt;= ' ~ 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?
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',
          '&gt;= ' ~ wage_from,
          '&lt;= ' ~ wage_to,
        ],
        startDate: [
          'and',
          '&gt;= ' ~ 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!