<p>Add the <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param" rel="nofollow"><code>relatedTo</code></a> param to your criteria</p>

CC BY-SA 3.0
2015-06-22T13:06:23.890Z
2015-06-22T13:06:23.890Z
Bob Olde Hampsink
<p>I am quite new to Craft and I am not sure how to get a prev/next link only for entries with related data in a specific field.</p>

<p>My situation:
I have a Structure (let's call it <code>sourceEntry</code>) with an optional field (can be empty or can have related data, let's call this one <code>relatedData</code>). That field is of type entries. So <code>sourceEntry</code> is related to other entries (I'll call them <code>targetEntry</code>).</p>

<p>Assuming I am on the entry's detail page (so, my <code>sourceEntry</code> object is just accessible via <code>entry</code> in my twig template) I would do this to link to previous/next entry:</p>

<p><code>
{% set criteria = {section: 'sourceEntry' } %}
{% set prev = entry.getPrev(criteria) %}
{% set next = entry.getNext(criteria) %}
{# …and then just insert the links… #}
</code></p>

<p>But in my case I need to get the prev/next entry that has set <code>relatedData</code> respectively I'd link to the prev/next entry that is related to <code>targetEntry</code>.</p>

<p>Example:<br/>
I have five entries of type <code>sourceEntry</code>: A, B, C, D, E.<br/>
The <code>relatedData</code> field for two of them is empty: B, C.<br/>
The others are related to posts of type <code>targetEntry</code>: A, D, E.<br/>
When loading the detail page for A there should be a next link to D, on detail page for D there should be a next link to E and a prev link to A and so on.</p>

<p>How am I able to achieve this? Thanks in advance!</p>

<p><strong>EDIT:</strong></p>

<p>This is the solution for those who are too lazy to click the link in Bob Olde Hampsink's answer:
<code>
{% set criteria = {section: 'sourceEntry', relatedTo: craft.users } %}
{% set prev = entry.getPrev(criteria) %}
{% set next = entry.getNext(criteria) %}
{# …and then just insert the links… #}
</code>
I feel kind of dumb now not even been trying this on my own before (although I knew about relations, but I wasn't sure how to set the criteria object properly). Thanks, Bob Olde Hampsink!</p>

<relations><previous-next>
How to link to prev/next entry depending on related data?
# How to link to prev/next entry depending on related data?

(Asked by: username_0 on 2015-06-22)

I am quite new to Craft and I am not sure how to get a prev/next link only for entries with related data in a specific field.

My situation:
I have a Structure (let's call it `sourceEntry`) with an optional field (can be empty or can have related data, let's call this one `relatedData`). That field is of type entries. So `sourceEntry` is related to other entries (I'll call them `targetEntry`).

Assuming I am on the entry's detail page (so, my `sourceEntry` object is just accessible via `entry` in my twig template) I would do this to link to previous/next entry:

`{% set criteria = {section: 'sourceEntry' } %}
{% set prev = entry.getPrev(criteria) %}
{% set next = entry.getNext(criteria) %}
{# …and then just insert the links… #}`

But in my case I need to get the prev/next entry that has set `relatedData` respectively I'd link to the prev/next entry that is related to `targetEntry`.

Example:  
I have five entries of type `sourceEntry`: A, B, C, D, E.  
The `relatedData` field for two of them is empty: B, C.  
The others are related to posts of type `targetEntry`: A, D, E.  
When loading the detail page for A there should be a next link to D, on detail page for D there should be a next link to E and a prev link to A and so on.

How am I able to achieve this? Thanks in advance!

**EDIT:**

This is the solution for those who are too lazy to click the link in username_1's answer:
`{% set criteria = {section: 'sourceEntry', relatedTo: craft.users } %}
{% set prev = entry.getPrev(criteria) %}
{% set next = entry.getNext(criteria) %}
{# …and then just insert the links… #}`
I feel kind of dumb now not even been trying this on my own before (although I knew about relations, but I wasn't sure how to set the criteria object properly). Thanks, username_1!

**Tags:** relations, previous-next


---

## 1 answers

### Answer by username_1 on 2015-06-22. Score: 1 | Accepted answer

Add the [`relatedTo`](http://buildwithcraft.com/docs/relations#the-relatedTo-param) param to your criteria


---

