<p>One nice way to handle things like this is to collect the ids for all the entries you want, use those ids for the <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow noreferrer">ElementCriteriaModel</a> you need for the pagination, and then paginate.</p>

<p>Collect the ids (use <a href="http://twig.sensiolabs.org/doc/2.x/filters/merge.html" rel="nofollow noreferrer">merge</a> to create an array of the ids):</p>

<pre><code>{% set ids = [] %}

{% for entry in craft.entries.section('news') %}
  {# loop through regions  #}  
  {% for region in entry.regions %}
    {# loop region's countries  #}
    {% for targetCountry in region.countries %}
      {% if targetCountry == currentUser.country %} 
        {% set ids = ids | merge([entry.id]) %}  
      {% endif %}
    {% endfor %}
  {% endfor %}
{% endfor %}
</code></pre>

<p>Get the entries for pagination:</p>

<pre><code>{% set unpaginated = craft.entries({
  id: ids,
  limit: 10
}) %}
</code></pre>

<p>Finally, the pagination:</p>

<pre><code>{% paginate unpaginated as pageInfo, pageEntries %}
...
</code></pre>

CC BY-SA 3.0
2017-01-17T23:15:43.477Z
2017-01-17T23:15:43.477Z
Marion Newlevant
<p>I'm really struggling with displaying news entry that are only relevant to the users' location.</p>

<p>Here is my structure:</p>

<p><strong>User</strong></p>

<p>Country - > is a dropdown with a single value (lets say UK)</p>

<p><strong>Regions</strong> is a category that contains</p>

<p>name -> string (for example Europe)</p>

<p>countries -> a checkbox with a list of countries (UK, FR.. etc)</p>

<p><strong>News</strong> which has a Region category</p>

<p>So what I have now is this:</p>

<pre><code>{% for entry in craft.entries.section('news') %}
{# loop through regions  #}  
    {% for region in entry.regions %}
        {# loop region's countries  #}
        {% for targetCountry in region.countries %}
            {% if targetCountry == currentUser.country %} 
                {% set visibility = true %}  
            {% endif %}
        {% endfor %}
    {% endfor %}
{% endfor %}
</code></pre>

<p>This works, however, since the checking occurs after I get all the entries I can't use pagination. Is there a nicer way of doing this?</p>

CC BY-SA 3.0
2017-01-17T15:01:25.580Z
Try changing the outer loop to the paginate tag `{% paginate craft.entries.section('news').relatedTo(entry).limit('12') as variablename %}` which doesn't need a closing tag or this link might help https://mijingo.com/blog/using-categories-in-craft-cms
mention79
CC BY-SA 3.0
2017-01-17T15:07:30.223Z
@mention79 , sorry don't get it, can you be a bit more specific, please?
Lukigi
CC BY-SA 3.0
2017-01-17T15:14:20.763Z
So refering specifically to the pagination point at the end of our post. I meant change the first line of code `{% for entry in craft.entries.section('news') %}` to something like `{% paginate craft.entries.section('news').relatedTo(entry).limit('12') as regions %}` the `.relatedTo(entry)` bit is to reference the categories linked to the entry and `.limit('12')` would give you 12 entries before it did a new page. And the final bit is the variable name that gets referenced in the for loop below. So `regions` and then in the one below it would change to `{% for region in regions %}`
mention79
CC BY-SA 3.0
2017-01-17T16:31:26.800Z
If I replace the first loop with your suggestion, I'm getting an error message because '.relatedTo(entry)' is relating to something that doesn't exist as there is no entry at this point. Am I missing something?
Lukigi
CC BY-SA 3.0
2017-01-17T16:55:43.720Z
Drop the `.relatedTo(entry)` possibly
mention79
CC BY-SA 3.0
2017-01-17T17:03:17.970Z
I see where you're going with it, but this will lead to unequal number of items on a page, as after dividing data into pages, some of it may or may not be displayed for the user country.
Lukigi
<entries><relations><pagination><craft-pro>
Display only entries relevant to users' country
# Display only entries relevant to users' country

(Asked by: username_0 on 2017-01-17)

I'm really struggling with displaying news entry that are only relevant to the users' location.
Here is my structure:
User
Country - > is a dropdown with a single value (lets say UK)
Regions is a category that contains
name -> string (for example Europe)
countries -> a checkbox with a list of countries (UK, FR.. etc)
News which has a Region category
So what I have now is this:
{% for entry in craft.entries.section('news') %}
{# loop through regions  #}  
    {% for region in entry.regions %}
        {# loop region's countries  #}
        {% for targetCountry in region.countries %}
            {% if targetCountry == currentUser.country %} 
                {% set visibility = true %}  
            {% endif %}
        {% endfor %}
    {% endfor %}
{% endfor %}

This works, however, since the checking occurs after I get all the entries I can't use pagination. Is there a nicer way of doing this?

**Tags:** entries, relations, pagination, craft-pro

> **Comments:**
> **username_1**: Try changing the outer loop to the paginate tag `{% paginate craft.entries.section('news').relatedTo(entry).limit('12') as variablename %}` which doesn't need a closing tag or this link might help https://mijingo.com/blog/using-categories-in-craft-cms  
> **username_0**: @username_1 , sorry don't get it, can you be a bit more specific, please?  
> **username_1**: So refering specifically to the pagination point at the end of our post. I meant change the first line of code `{% for entry in craft.entries.section('news') %}` to something like `{% paginate craft.entries.section('news').relatedTo(entry).limit('12') as regions %}` the `.relatedTo(entry)` bit is to reference the categories linked to the entry and `.limit('12')` would give you 12 entries before it did a new page. And the final bit is the variable name that gets referenced in the for loop below. So `regions` and then in the one below it would change to `{% for region in regions %}`  
> **username_0**: If I replace the first loop with your suggestion, I'm getting an error message because '.relatedTo(entry)' is relating to something that doesn't exist as there is no entry at this point. Am I missing something?  
> **username_1**: Drop the `.relatedTo(entry)` possibly  
> **username_0**: I see where you're going with it, but this will lead to unequal number of items on a page, as after dividing data into pages, some of it may or may not be displayed for the user country.  

---

## 1 answers

### Answer by username_2 on 2017-01-17. Score: 1 | Accepted answer

One nice way to handle things like this is to collect the ids for all the entries you want, use those ids for the ElementCriteriaModel you need for the pagination, and then paginate.
Collect the ids (use merge to create an array of the ids):
{% set ids = [] %}

{% for entry in craft.entries.section('news') %}
  {# loop through regions  #}  
  {% for region in entry.regions %}
    {# loop region's countries  #}
    {% for targetCountry in region.countries %}
      {% if targetCountry == currentUser.country %} 
        {% set ids = ids | merge([entry.id]) %}  
      {% endif %}
    {% endfor %}
  {% endfor %}
{% endfor %}

Get the entries for pagination:
{% set unpaginated = craft.entries({
  id: ids,
  limit: 10
}) %}

Finally, the pagination:
{% paginate unpaginated as pageInfo, pageEntries %}
...


---

