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?