<p>Building on <a href="https://craftcms.stackexchange.com/a/646/130">this answer</a>, and <a href="https://craftcms.stackexchange.com/questions/1948/complex-logic-on-a-elementcriteriamodel-parameter/4575#4575">this question</a>, here is what I would try:</p>

<p>Get the posts from the current city:</p>

<pre><code>{% set currentCityPostIds = craft.entries.section('news').relatedTo(currentCityFilter).ids() %}
</code></pre>

<p>Get the posts with no <code>city</code> (uses <a href="https://craftcms.com/docs/searching" rel="nofollow noreferrer">search</a>):</p>

<pre><code>{% set noCityPostIds = craft.entries.section('news').search('-city:*').ids %}
</code></pre>

<p>Make one list of all the ids (twig <a href="http://twig.sensiolabs.org/doc/filters/merge.html" rel="nofollow noreferrer">merge filter</a>):</p>

<pre><code>{% set postIds = currentCityPostIds|merge(noCityPostIds) %}
</code></pre>

<p>Get all the posts with those ids:</p>

<pre><code>{% set postsToPaginate = craft.entries.id(postIds) %}
</code></pre>

<p>Now you can paginate them:</p>

<pre><code>{% paginate postsToPaginate.order("featured desc").limit(10) as pageInfo, pagePosts %}
</code></pre>

CC BY-SA 3.0
2016-02-13T01:41:45.923Z
2016-02-13T01:41:45.923Z
2017-04-13T12:50:59.930Z
Marion Newlevant
<p>I'm struggling to get my head around relations. It seems overly complex for the simple thing I want to do.</p>

<p>I am using an entry field on my post to set relations by city. These entries are limited to a structure that has a list of cities. I am outputting a global variable that is the current location (city).</p>

<p>With the following code, I am able to get any posts from the current city.</p>

<pre><code>{% paginate craft.entries.section("news").relatedTo(currentCityFilter).order("featured desc").limit(10) as pageInfo, pagePosts %}

{% for post in pagePosts %}
    &lt;article&gt;
        &lt;h3&gt;&lt;a href="{{ post.url }}"&gt;{{ post.title }}&lt;/a&gt;&lt;/h3&gt;
        &lt;p&gt;Posted on {{ post.postDate.format("F d, Y") }}&lt;/p&gt;
        {{ post.body.getPage(1) }}
        &lt;p&gt;&lt;a href="{{ post.url }}"&gt;Continue reading&lt;/a&gt;&lt;/p&gt;
    &lt;/article&gt;
{% endfor %}
</code></pre>

<p>The problem is, I don't want to force the user to set every city when they wish to have a post display for every location.</p>

<p>What's the proper way to show any posts that don't have a city set and still filter posts that do have a city by the current city?</p>

<p>Similar question with no answer here: <a href="https://craftcms.stackexchange.com/questions/12391/get-entries-where-relation-matches-or-is-empty">Get entries where relation matches or is empty</a></p>

<relations>
Show posts related to entry AND all posts not related to any entry
# Show posts related to entry AND all posts not related to any entry

(Asked by: username_0 on 2016-02-12)

I'm struggling to get my head around relations. It seems overly complex for the simple thing I want to do.
I am using an entry field on my post to set relations by city. These entries are limited to a structure that has a list of cities. I am outputting a global variable that is the current location (city).
With the following code, I am able to get any posts from the current city.
{% paginate craft.entries.section("news").relatedTo(currentCityFilter).order("featured desc").limit(10) as pageInfo, pagePosts %}

{% for post in pagePosts %}
    
        {{ post.title }}
        Posted on {{ post.postDate.format("F d, Y") }}
        {{ post.body.getPage(1) }}
        Continue reading
    
{% endfor %}

The problem is, I don't want to force the user to set every city when they wish to have a post display for every location.
What's the proper way to show any posts that don't have a city set and still filter posts that do have a city by the current city?
Similar question with no answer here: Get entries where relation matches or is empty

**Tags:** relations

---

## 1 answers

### Answer by username_1 on 2016-02-13. Score: 2 | Accepted answer

Building on this answer, and this question, here is what I would try:
Get the posts from the current city:
{% set currentCityPostIds = craft.entries.section('news').relatedTo(currentCityFilter).ids() %}

Get the posts with no city (uses search):
{% set noCityPostIds = craft.entries.section('news').search('-city:*').ids %}

Make one list of all the ids (twig merge filter):
{% set postIds = currentCityPostIds|merge(noCityPostIds) %}

Get all the posts with those ids:
{% set postsToPaginate = craft.entries.id(postIds) %}

Now you can paginate them:
{% paginate postsToPaginate.order("featured desc").limit(10) as pageInfo, pagePosts %}


---

