<p>We just released Element API 2.6.0 with added support for the <code>offset</code> criteria param, in combination with <code>paginate =&gt; true</code>.</p>

<p>(Thanks to @missmatsuko for posting a GitHub issue about it: <a href="https://github.com/craftcms/element-api/issues/99" rel="nofollow noreferrer">https://github.com/craftcms/element-api/issues/99</a>)</p>

CC BY-SA 4.0
2019-08-01T22:52:55.523Z
2019-08-01T22:52:55.523Z
Brandon Kelly
<p>It seems that <code>offset</code> doesn't work when <code>pagination</code> is set to <code>true</code> (which is its default value). </p>

<p><strong>If you don't need pagination for your results</strong>, you can add your <code>offset</code> in <code>criteria</code> and set <code>pagination</code> to <code>false</code>.</p>

<p><strong>If you do need pagination and the desired <code>offset</code> is the same as <code>elementsPerPage</code></strong>, you can get your results starting from page 2. You'll have to make sure you're actually getting page 2 because it will return page 1 if there aren't enough results for a second page.</p>

<p><strong>If you do need pagination and the desired <code>offset</code> is different from <code>elementsPerPage</code></strong>, you can try what I've done here.</p>

<pre><code>'stories.json' =&gt; function() {
    // Get get IDs of the "offset" entries. Set `limit` to the offset amount you want.
    $excludedIds = Entry::find()
        -&gt;section('stories')
        -&gt;limit(4)
        -&gt;ids();

    // Form value for id in criteria to exclude the above entries.
    $criteriaId = count($excludedIds) ? ('and, not ' . implode(', not ', $excludedIds)) : null;

    return [
        'elementType' =&gt; Entry::class,
        'criteria' =&gt; [
            'section' =&gt; 'stories',
            'id' =&gt; $criteriaId,
        ],
        'pretty' =&gt; true,
        'elementsPerPage' =&gt; 6,
        'transformer' =&gt; function(Entry $entry) {
            return [
                'title' =&gt; $entry-&gt;title,
                'url' =&gt; $entry-&gt;url,
                'id' =&gt; $entry-&gt;id,
            ];
        },
    ];
},
</code></pre>

<p>I found that you can still paginate results with a value for <code>id</code> set in <code>criteria</code>. So, I've excluded the IDs of the offset entries and paginated the results with those entries removed.</p>

<p>I've also made an issue on the Element API Github repo to see if there's a better solution:
<a href="https://github.com/craftcms/element-api/issues/99" rel="nofollow noreferrer">https://github.com/craftcms/element-api/issues/99</a></p>

CC BY-SA 4.0
2019-07-20T02:53:58.460Z
This answer might be for Craft 3.  For Craft 2 use the following element query:

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'news';
$criteria->limit = 1;
$excludedIds = $criteria->ids();
biko
CC BY-SA 4.0
2019-02-13T23:48:18.413Z
2019-02-15T20:36:08.513Z
2019-02-15T20:36:08.513Z
missmatsuko
<p>I'm looking how to use the <code>.offset(x)</code> parameters of craft.entries inside the ElementAPI.</p>

<p>I tried to add <code>'offset' =&gt;</code> 1 before, after and inside the <code>criteria</code> array, but the x first entries are still added to the JSON.</p>

<p>How can I skip the first X entries of ElementAPI?</p>

CC BY-SA 4.0
2019-02-13T22:32:59.783Z
It seems that `offset` only works when `pagination` is set to `false`.
missmatsuko
<plugin-development><entries><plugin-elementapi><json>
How to skip the first X entries using ElementAPI
# How to skip the first X entries using ElementAPI

(Asked by: username_0 on 2017-04-12)

I'm looking how to use the `.offset(x)` parameters of craft.entries inside the ElementAPI.

I tried to add `'offset' =>` 1 before, after and inside the `criteria` array, but the x first entries are still added to the JSON.

How can I skip the first X entries of ElementAPI?

**Tags:** plugin-development, entries, plugin-elementapi, json

> **Comments:**
> **username_1**: It seems that `offset` only works when `pagination` is set to `false`.  

---

## 2 answers

### Answer by username_2 on 2019-08-01. Score: 2 | Accepted answer

We just released Element API 2.6.0 with added support for the `offset` criteria param, in combination with `paginate => true`.

(Thanks to @username_1 for posting a GitHub issue about it: <https://github.com/craftcms/element-api/issues/99>)


---

### Answer by username_1 on 2019-02-13. Score: 3

It seems that offset doesn't work when pagination is set to true (which is its default value). 
If you don't need pagination for your results, you can add your offset in criteria and set pagination to false.
If you do need pagination and the desired offset is the same as elementsPerPage, you can get your results starting from page 2. You'll have to make sure you're actually getting page 2 because it will return page 1 if there aren't enough results for a second page.
If you do need pagination and the desired offset is different from elementsPerPage, you can try what I've done here.
'stories.json' => function() {
    // Get get IDs of the "offset" entries. Set `limit` to the offset amount you want.
    $excludedIds = Entry::find()
        ->section('stories')
        ->limit(4)
        ->ids();

    // Form value for id in criteria to exclude the above entries.
    $criteriaId = count($excludedIds) ? ('and, not ' . implode(', not ', $excludedIds)) : null;

    return [
        'elementType' => Entry::class,
        'criteria' => [
            'section' => 'stories',
            'id' => $criteriaId,
        ],
        'pretty' => true,
        'elementsPerPage' => 6,
        'transformer' => function(Entry $entry) {
            return [
                'title' => $entry->title,
                'url' => $entry->url,
                'id' => $entry->id,
            ];
        },
    ];
},

I found that you can still paginate results with a value for id set in criteria. So, I've excluded the IDs of the offset entries and paginated the results with those entries removed.
I've also made an issue on the Element API Github repo to see if there's a better solution:
https://github.com/craftcms/element-api/issues/99

> **Comments:**
> **username_3**: This answer might be for Craft 3.  For Craft 2 use the following element query:  $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->section = 'news'; $criteria->limit = 1; $excludedIds = $criteria->ids();  

---

