<pre><code>{% set range = entry.model %}
{% set categories = craft.categories.slug('not preferential') %}
{% set entries = craft.entries.section('offers').search('title:' ~ range).relatedTo(categories) %}
</code></pre>

<p>If you are only searching titles I would add that in the search params as it will help the effectiveness of the search. In addition when setting a variable you do not need to use the <code>{{}}</code> syntax as it will automatically use the string representation of the the field if it is a text field.</p>

CC BY-SA 3.0
2016-05-04T13:07:13.910Z
Excellent, thanks a lot! I'll look into search filters more so I fully understand it for next time. Thanks!
JonnyT
CC BY-SA 3.0
2016-05-04T13:07:48.927Z
This page shows the various filters and what fields can be targeted for each element type. https://craftcms.com/docs/searching
mcclaskiem
CC BY-SA 3.0
2016-05-04T13:09:30.263Z
Yeah read that page but where I was going wrong was adding 'title:range' within the search brackets as opposed to ('title:' ~ range).
JonnyT
CC BY-SA 3.0
2016-05-04T13:12:56.640Z
Just an additional observation, if all entries have a "car model" you could make that a `category` field allowing for a `relatedTo` query which is much quicker. As you get more entries search will definitely underperform compared to a `relatedTo` query.
mcclaskiem
CC BY-SA 3.0
2016-05-04T13:22:21.450Z
Considered that first but the feed gets updated daily and not really sure how to import entries along with categories just yet so it seemed the way to go. There are about 180 entries at any one time so hopefully that should be OK.
JonnyT
CC BY-SA 3.0
2016-05-04T13:27:51.883Z
Depending on how you are importing I could probably show you how to do it pretty easily. Are you in Craft Slack?
mcclaskiem
CC BY-SA 3.0
2016-05-04T13:35:39.637Z
It's from a daily CSV file sent each night. Yeah that'll be great, I'll open it now.
JonnyT
CC BY-SA 3.0
2016-05-04T13:04:26.753Z
2016-05-04T13:04:26.753Z
mcclaskiem
<p>I've got an entry page and at the bottom I would like to show related products and then underneath that, related offers. </p>

<p>As the main page entry isn't assigned to any categories I've tried to use a custom field and the search param to show 'related' entries and this works fine, BUT, when I try and make it dynamic, it doesn't pull through. I'm guessing this is some kind of parsing order and that the custom field isn't parsed before the loop is created.</p>

<p>This works fine: </p>

<pre><code>{# To exclude certain category #}
{% set categories = craft.categories.slug('not preferential') %}
{% set entries = craft.entries.section('offers').search('A1').relatedTo(categories) %}
</code></pre>

<p>But this doesn't when I try and make it dynamic:</p>

<pre><code>{% set categories = craft.categories.slug('not preferential') %}
{% set entries = craft.entries.section('offers').search('{{ entry.model }}').relatedTo(categories) %}
</code></pre>

<p>I've also tried setting the variable first but it doesn't work:</p>

<pre><code>{% set range = "{{ entry.model }}" %}
{% set categories = craft.categories.slug('not preferential') %}
{% set entries = craft.entries.section('offers').search(range).relatedTo(categories) %}
</code></pre>

<p>Hopefully this makes sense. Still in the basics I suppose of Craft but worth the learning curve. </p>

CC BY-SA 3.0
2016-05-04T12:42:56.310Z
Is A1 a title field or what would be a specific field on the model?
mcclaskiem
CC BY-SA 3.0
2016-05-04T12:52:13.780Z
Sorry, 'A1' is the model of the Audi vehicle so in this case I want to search all other offers that have 'A1' in the title e.g. 'New Audi A1...' etc.
JonnyT
CC BY-SA 3.0
2016-05-04T12:58:33.803Z
try adding `.search('title:' ~ range)` where range is the variable you set however your `set` needs to be `= entry.model` not the echo syntax. This question is somewhat similar as well http://craftcms.stackexchange.com/questions/9301/how-to-query-using-variables
mcclaskiem
CC BY-SA 3.0
2016-05-04T13:06:41.627Z
I've been searching yesterday and today and trying loads of various ways but never saw the post above. :/
JonnyT
<entries><categories><relations><field><search>
Using the Search Filter with Entry Fields
# Using the Search Filter with Entry Fields

(Asked by: username_0 on 2016-05-04)

I've got an entry page and at the bottom I would like to show related products and then underneath that, related offers. 
As the main page entry isn't assigned to any categories I've tried to use a custom field and the search param to show 'related' entries and this works fine, BUT, when I try and make it dynamic, it doesn't pull through. I'm guessing this is some kind of parsing order and that the custom field isn't parsed before the loop is created.
This works fine: 
{# To exclude certain category #}
{% set categories = craft.categories.slug('not preferential') %}
{% set entries = craft.entries.section('offers').search('A1').relatedTo(categories) %}

But this doesn't when I try and make it dynamic:
{% set categories = craft.categories.slug('not preferential') %}
{% set entries = craft.entries.section('offers').search('{{ entry.model }}').relatedTo(categories) %}

I've also tried setting the variable first but it doesn't work:
{% set range = "{{ entry.model }}" %}
{% set categories = craft.categories.slug('not preferential') %}
{% set entries = craft.entries.section('offers').search(range).relatedTo(categories) %}

Hopefully this makes sense. Still in the basics I suppose of Craft but worth the learning curve.

**Tags:** entries, categories, relations, field, search

> **Comments:**
> **username_1**: Is A1 a title field or what would be a specific field on the model?  
> **username_0**: Sorry, 'A1' is the model of the Audi vehicle so in this case I want to search all other offers that have 'A1' in the title e.g. 'New Audi A1...' etc.  
> **username_1**: try adding `.search('title:' ~ range)` where range is the variable you set however your `set` needs to be `= entry.model` not the echo syntax. This question is somewhat similar as well http://craftcms.stackexchange.com/questions/9301/how-to-query-using-variables  
> **username_0**: I've been searching yesterday and today and trying loads of various ways but never saw the post above. :/  

---

## 1 answers

### Answer by username_1 on 2016-05-04. Score: 3 | Accepted answer

{% set range = entry.model %}
{% set categories = craft.categories.slug('not preferential') %}
{% set entries = craft.entries.section('offers').search('title:' ~ range).relatedTo(categories) %}

If you are only searching titles I would add that in the search params as it will help the effectiveness of the search. In addition when setting a variable you do not need to use the {{}} syntax as it will automatically use the string representation of the the field if it is a text field.

> **Comments:**
> **username_0**: Excellent, thanks a lot! I'll look into search filters more so I fully understand it for next time. Thanks!  
> **username_1**: This page shows the various filters and what fields can be targeted for each element type. https://craftcms.com/docs/searching  
> **username_0**: Yeah read that page but where I was going wrong was adding 'title:range' within the search brackets as opposed to ('title:' ~ range).  
> **username_1**: Just an additional observation, if all entries have a "car model" you could make that a `category` field allowing for a `relatedTo` query which is much quicker. As you get more entries search will definitely underperform compared to a `relatedTo` query.  
> **username_0**: Considered that first but the feed gets updated daily and not really sure how to import entries along with categories just yet so it seemed the way to go. There are about 180 entries at any one time so hopefully that should be OK.  
> **username_1**: Depending on how you are importing I could probably show you how to do it pretty easily. Are you in Craft Slack?  
> **username_0**: It's from a daily CSV file sent each night. Yeah that'll be great, I'll open it now.  

---

