<pre class="lang-twig prettyprint-override"><code>{% set location = block.locationVacancies %}
{% set entries = craft
    .entries
    .section('careers')
    .search('locationVacancies:"' ~ location ~ '"')
    .find()
%}
</code></pre>

CC BY-SA 4.0
2018-10-23T06:08:29.900Z
2018-10-23T06:08:29.900Z
Robin Schambach
<p>Twig has an <a href="https://twig.symfony.com/doc/2.x/templates.html#string-interpolation" rel="nofollow noreferrer">interpolation syntax</a> (using <code>#{}</code> inside a double-quoted string) to accomplish this:</p>

<pre><code>{% set location = block.locationVacancies %}
{% set entries = craft
    .entries
    .section('careers')
    .search("locationVacancies:#{location}")
    .all() %}
</code></pre>

<hr>

<p>In addition, you can be more explicit about the values you’re searching for (and avoid any kind of sneaky <code>search</code> capabilities/side-effects exposed by using raw query input):</p>

<pre><code>{% set location = block.locationVacancies %}
{% set entries = craft
    .entries
    .section('careers')
    .locationVacancies("*#{location}*")
    .all() %}
</code></pre>

<p>…or even using Twig object syntax…</p>

<pre><code>{% set location = block.locationVacancies %}
{% set entries = craft.entries({
    section: 'careers',
    locationVacancies: "*#{location}*"
}).all() %}
</code></pre>

<p>(<code>locationVacancies: location</code> will also work, but disables partial matching)</p>

<blockquote>
  <p>It should be noted: if you're looking to associate multiple explicit entries, you can also make the field within your (presumed) Matrix Block an <code>Entries</code> field, which establishes actual relationships in the database—accessing such a field (<code>myBlock.myLocationEntriesField.all()</code>) will return that list of Entries, exactly as set in the admin) If you just need a dynamic search param, what you have is a fine solution!</p>
</blockquote>

CC BY-SA 4.0
2018-10-24T06:15:26.790Z
2018-10-30T16:44:29.383Z
2018-10-30T16:44:29.383Z
August Miller
<p>How do I filter a list of entries by the value of a field using the value of a variable? </p>

<p>e.g.</p>

<pre><code>{% set location = block.locationVacancies %}
{% set entries = craft.entries.section('careers').search('locationVacancies:"{{location}}"')
</code></pre>

<p>doesn't work for me. But if I replace <code>{{location}}</code> with the value of the string <code>location</code> by hand then it works</p>

<pre><code>{% set entries = craft.entries.section('careers').search('locationVacancies:"uk"')
</code></pre>

<entries><search><variable><filtering>
Filter entries by Field value using a variable
# Filter entries by Field value using a variable

(Asked by: username_0 on 2018-10-23)

How do I filter a list of entries by the value of a field using the value of a variable? 
e.g.
{% set location = block.locationVacancies %}
{% set entries = craft.entries.section('careers').search('locationVacancies:"{{location}}"')

doesn't work for me. But if I replace {{location}} with the value of the string location by hand then it works
{% set entries = craft.entries.section('careers').search('locationVacancies:"uk"')

**Tags:** entries, search, variable, filtering


---

## 2 answers

### Answer by username_1 on 2018-10-23. Score: 2

{% set location = block.locationVacancies %}
{% set entries = craft
    .entries
    .section('careers')
    .search('locationVacancies:"' ~ location ~ '"')
    .find()
%}


---

### Answer by username_2 on 2018-10-24. Score: 2

Twig has an interpolation syntax (using #{} inside a double-quoted string) to accomplish this:
{% set location = block.locationVacancies %}
{% set entries = craft
    .entries
    .section('careers')
    .search("locationVacancies:#{location}")
    .all() %}


In addition, you can be more explicit about the values you’re searching for (and avoid any kind of sneaky search capabilities/side-effects exposed by using raw query input):
{% set location = block.locationVacancies %}
{% set entries = craft
    .entries
    .section('careers')
    .locationVacancies("*#{location}*")
    .all() %}

…or even using Twig object syntax…
{% set location = block.locationVacancies %}
{% set entries = craft.entries({
    section: 'careers',
    locationVacancies: "*#{location}*"
}).all() %}

(locationVacancies: location will also work, but disables partial matching)

It should be noted: if you're looking to associate multiple explicit entries, you can also make the field within your (presumed) Matrix Block an Entries field, which establishes actual relationships in the database—accessing such a field (myBlock.myLocationEntriesField.all()) will return that list of Entries, exactly as set in the admin) If you just need a dynamic search param, what you have is a fine solution!


---

