<p>If I correctly understand your question, you want to show each location with at least one vacancy, and the number of vacancies for that location? I don't really get what <code>block.vacancySelected</code> is?</p>

<p>The way I would do this:</p>

<pre><code>{# get all locations #}
{% set allLocations = craft.entries.section('locations').all() %}

{% for location in location %}
    {# get a vacancy count for this location #}
    {% set vacancyCountForLocation = craft.entries.section('vacancies').relatedTo({
        field: 'locationFieldHandle',
        targetElement: location,
    }).count() %}

    {% if vacancyCountForLocation %}
        {{ location.locationName }} ({{ vacancyCountForLocation }})
    {% endfor %}
{% endfor %}
</code></pre>

<p>To prevent loading all locations, even if they have no vacancies, you could also try (untested):</p>

<pre><code>{# get all locations, but only if they have related vacancies #}
{% set allVacancies = craft.entries.section('vacancies').ids() %}
{% set allLocations = craft.entries.section('locations').relatedTo({
    field: 'locationFieldHandle',
    sourceElement: allVacancies,
}).all() %}

{% for location in location %}
    {# get a vacancy count for this location #}
    {% set vacancyCountForLocation = craft.entries.section('vacancies').relatedTo({
        field: 'locationFieldHandle',
        targetElement: location,
    }).count() %}

    {{ location.locationName }} ({{ vacancyCountForLocation }})
{% endfor %}
</code></pre>

CC BY-SA 4.0
2018-11-08T13:03:56.920Z
Thank you, yes you understood it correctly. `block.vacancySelected` is a set of vacancies – we only want to show selected vacancies. We want to display each location with at least one of the selected vacancies.
user9247
CC BY-SA 4.0
2018-11-07T13:01:56.500Z
2018-11-07T13:18:14.817Z
2018-11-07T13:18:14.817Z
Paul
<p>There's a section called <code>locations</code> and another called <code>vacancies</code>. In <code>vacancies</code> the admin can assign a <code>location</code> entry to a <code>vacancy</code>. </p>

<p>On the careers overview page we want to display selected vacancies <code>block.vacancySelected</code>.</p>

<p>In the template we loop through all locations and would like to display the number of vacancies per location. </p>

<pre><code>{% set lparas = {
    section: 'locations'
    } %}
{% set locations = craft.entries(lparas) %}

{% if locations | length %}
   {% for location in locations %}
      {% set cEntries = block.vacancySelected.relatedTo(location) %}
      {% if cEntries| length %}
         {{ location.locationName }} ({{ cEntries | length }})
      {% endif %}
   {% endfor %}
{% endif %}
</code></pre>

<p>But for some reason it shows all locations and the total number of vacancies per locations. It seems to ignore that we only want to show the locations of the selected vacancies.</p>

CC BY-SA 4.0
2018-11-07T13:00:17.253Z
Where does the `block` variable come from?
Robin Schambach
<entries><relations><filter><relatedto>
Filter entries by other entry
# Filter entries by other entry

(Asked by: username_0 on 2018-11-07)

There's a section called locations and another called vacancies. In vacancies the admin can assign a location entry to a vacancy. 
On the careers overview page we want to display selected vacancies block.vacancySelected.
In the template we loop through all locations and would like to display the number of vacancies per location. 
{% set lparas = {
    section: 'locations'
    } %}
{% set locations = craft.entries(lparas) %}

{% if locations | length %}
   {% for location in locations %}
      {% set cEntries = block.vacancySelected.relatedTo(location) %}
      {% if cEntries| length %}
         {{ location.locationName }} ({{ cEntries | length }})
      {% endif %}
   {% endfor %}
{% endif %}

But for some reason it shows all locations and the total number of vacancies per locations. It seems to ignore that we only want to show the locations of the selected vacancies.

**Tags:** entries, relations, filter, relatedto
> **Comments:**
> **username_1**: Where does the `block` variable come from?  

---

## 1 answers

### Answer by username_2 on 2018-11-07. Score: 2

If I correctly understand your question, you want to show each location with at least one vacancy, and the number of vacancies for that location? I don't really get what block.vacancySelected is?
The way I would do this:
{# get all locations #}
{% set allLocations = craft.entries.section('locations').all() %}

{% for location in location %}
    {# get a vacancy count for this location #}
    {% set vacancyCountForLocation = craft.entries.section('vacancies').relatedTo({
        field: 'locationFieldHandle',
        targetElement: location,
    }).count() %}

    {% if vacancyCountForLocation %}
        {{ location.locationName }} ({{ vacancyCountForLocation }})
    {% endfor %}
{% endfor %}

To prevent loading all locations, even if they have no vacancies, you could also try (untested):
{# get all locations, but only if they have related vacancies #}
{% set allVacancies = craft.entries.section('vacancies').ids() %}
{% set allLocations = craft.entries.section('locations').relatedTo({
    field: 'locationFieldHandle',
    sourceElement: allVacancies,
}).all() %}

{% for location in location %}
    {# get a vacancy count for this location #}
    {% set vacancyCountForLocation = craft.entries.section('vacancies').relatedTo({
        field: 'locationFieldHandle',
        targetElement: location,
    }).count() %}

    {{ location.locationName }} ({{ vacancyCountForLocation }})
{% endfor %}

> **Comments:**
> **username_0**: Thank you, yes you understood it correctly. `block.vacancySelected` is a set of vacancies – we only want to show selected vacancies. We want to display each location with at least one of the selected vacancies.  

---

