<p>Depends on what you mean by “next”.</p>

<p>If the entry is within a Structure section and you want whatever entry is positioned after it, you’d use <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#getNextSibling-detail" rel="nofollow">getNextSibling()</a>.</p>



<pre class="lang-php prettyprint-override"><code>$nextSibling = $entry-&gt;getNextSibling();
</code></pre>

<p>Otherwise you want <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#getNext-detail" rel="nofollow">getNext()</a>. But you need to keep in mind that <em>which</em> entry is next can vary in infinite ways, so you really need to supply that <code>$criteria</code> argument here.</p>

<p>The point of the <code>$criteria</code> argument is to define a collection of entries in a specific order, in which <code>$entry</code> is one of them, and Craft uses that to determine which entries come immediately before and after it.</p>

<p>Your example code shows that you are returning Journal entries, so I’ll assume that you want <code>$nextEntry</code> to be the journal entry that was written <em>after</em> <code>$entry</code> – so the entries should be sorted in chronological order. If that’s the case, here’s the code you should use:</p>

<pre class="lang-php prettyprint-override"><code>$nextEntry = $entry-&gt;getNext([
    'section' =&gt; 'journal',
    'order' =&gt; 'postDate asc'
]);
</code></pre>

<p>As far as what you can pass into that argument, all of the <a href="http://buildwithcraft.com/docs/templating/craft.entries#parameters" rel="nofollow">craft.entries parameters</a> are fair game here, with the exact same value syntax. Just keep in mind that it’s PHP code and not Twig, so instead of something like:</p>

<pre class="lang-php prettyprint-override"><code>.postDate('&lt;= 2013-06-04')
</code></pre>

<p>it would be:</p>

<pre class="lang-php prettyprint-override"><code>'postDate' =&gt; '&lt;= 2013-06-04',
</code></pre>

<p>You can also pass in an actual <a href="http://buildwithcraft.com/classreference/models/ElementCriteriaModel" rel="nofollow">ElementCriteriaModel</a> object, with its values set ahead of time:</p>

<pre class="lang-php prettyprint-override"><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'journal';
$criteria-&gt;postDate = '&lt;= 2013-06-04';
$criteria-&gt;order = 'postDate asc';

$nextEntry = $entry-&gt;getNext($criteria);
</code></pre>

CC BY-SA 3.0
2015-08-27T18:15:59.877Z
Works great, thank you!  Quick followup, where is it documented what options I have for the $criteria argument?  e.g. section journal, order postDate, postdate <= otherDate?  I'm having trouble finding it in the Craft documentation.
Jonathan Walters
CC BY-SA 3.0
2015-08-27T18:54:58.647Z
@JonathanWalters Just updated my answer with more info about that.
Brandon Kelly
CC BY-SA 3.0
2015-08-27T18:01:08.210Z
2015-08-27T18:54:44.423Z
2015-08-27T18:54:44.423Z
Brandon Kelly
<pre><code>return [
    'endpoints' =&gt; [

        'journal/&lt;entryId:\d+&gt;.json' =&gt; function($entryId) {
            return [
                'elementType' =&gt; 'Entry',
                'criteria' =&gt; ['id' =&gt; $entryId, 'section'=&gt;'journal'],
                'first' =&gt; true,
                'transformer' =&gt; function(EntryModel $entry) {
                    $nextEntry = $entry-&gt;getNextSibling();
                    // also tried getNext()

                    $upNext = [];
                    $upNext["title"] = $nextEntry-&gt;title;
                    // errs out here ...
                }
            ];
        }
    ]
];
</code></pre>

<p>I feel like I'm missing something obvious, but how do I get the next sibling of an entry using Element API?  Would it be easier/better to pass both the current entry ID and the next entry ID (which I have available) into the criteria to make it easier to grab?</p>

<p>Thanks!</p>

CC BY-SA 3.0
2015-08-27T17:03:31.280Z
Just to check, 'journal' is a Structure section and you're only looking to return one entry, correct?
Brad Bell
<plugin-elementapi>
Using Element API, how do I get the next entry?
# Using Element API, how do I get the next entry?

(Asked by: username_0 on 2015-08-27)

return [
    'endpoints' => [

        'journal/.json' => function($entryId) {
            return [
                'elementType' => 'Entry',
                'criteria' => ['id' => $entryId, 'section'=>'journal'],
                'first' => true,
                'transformer' => function(EntryModel $entry) {
                    $nextEntry = $entry->getNextSibling();
                    // also tried getNext()

                    $upNext = [];
                    $upNext["title"] = $nextEntry->title;
                    // errs out here ...
                }
            ];
        }
    ]
];

I feel like I'm missing something obvious, but how do I get the next sibling of an entry using Element API?  Would it be easier/better to pass both the current entry ID and the next entry ID (which I have available) into the criteria to make it easier to grab?
Thanks!

**Tags:** plugin-elementapi

> **Comments:**
> **username_1**: Just to check, 'journal' is a Structure section and you're only looking to return one entry, correct?  

---

## 1 answers

### Answer by username_2 on 2015-08-27. Score: 3 | Accepted answer

Depends on what you mean by “next”.
If the entry is within a Structure section and you want whatever entry is positioned after it, you’d use getNextSibling().
$nextSibling = $entry->getNextSibling();

Otherwise you want getNext(). But you need to keep in mind that which entry is next can vary in infinite ways, so you really need to supply that $criteria argument here.
The point of the $criteria argument is to define a collection of entries in a specific order, in which $entry is one of them, and Craft uses that to determine which entries come immediately before and after it.
Your example code shows that you are returning Journal entries, so I’ll assume that you want $nextEntry to be the journal entry that was written after $entry – so the entries should be sorted in chronological order. If that’s the case, here’s the code you should use:
$nextEntry = $entry->getNext([
    'section' => 'journal',
    'order' => 'postDate asc'
]);

As far as what you can pass into that argument, all of the craft.entries parameters are fair game here, with the exact same value syntax. Just keep in mind that it’s PHP code and not Twig, so instead of something like:
.postDate('<= 2013-06-04')

it would be:
'postDate' => '<= 2013-06-04',

You can also pass in an actual ElementCriteriaModel object, with its values set ahead of time:
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'journal';
$criteria->postDate = '<= 2013-06-04';
$criteria->order = 'postDate asc';

$nextEntry = $entry->getNext($criteria);

> **Comments:**
> **username_0**: Works great, thank you!  Quick followup, where is it documented what options I have for the $criteria argument?  e.g. section journal, order postDate, postdate <= otherDate?  I'm having trouble finding it in the Craft documentation.  
> **username_2**: @JonathanWalters Just updated my answer with more info about that.  

---

