<p>The <code>implode</code> function will turn the array of IDs into a comma-separated string. Then you're putting that into an array with <code>'not'</code> as the first element, so the parameter you're passing to <code>id()</code> looks like this:</p>
<pre class="lang-php prettyprint-override"><code>['not', '1602,1630']
</code></pre>
<p>This isn't one of the <a href="https://craftcms.com/docs/4.x/entries.html#id" rel="nofollow noreferrer">supported formats</a> – if you want to exclude a list of IDs, you pass an array of IDs with <code>'not'</code> as the first element:</p>
<pre class="lang-php prettyprint-override"><code>['not', 1602, 1630]
</code></pre>
<p>Instead of using <code>implode</code>, you can use the array returned by your first query as-is, only adding <code>'not'</code> at the beginning using <a href="https://www.php.net/manual/en/function.array-unshift.php" rel="nofollow noreferrer"><code>array_unshift</code></a>:</p>
<pre class="lang-php prettyprint-override"><code>$excluded = $entry-&gt;featuredMedia-&gt;ids();
array_unshift($excluded, 'not');

$entries = Entry::find()
    -&gt;section('blog')
    -&gt;id($excluded)
    -&gt;all();
</code></pre>

CC BY-SA 4.0
2023-06-23T16:49:08.297Z
That worked perfectly and seems obvious in retrospect. Thanks for the help!
Dalton Rooney
CC BY-SA 4.0
2023-06-23T15:14:03.047Z
2023-06-23T15:14:03.047Z
MoritzLost
<p>This seems more like a PHP question than a Craft CMS question, but since it's related to an entry query I'm trying to create I figured someone from the Craft community might know. I'm having trouble creating a query that excludes some IDs that are pulled from another entry field. (It's a blog with a &quot;Featured Entries&quot; field at the top, if there are featured entries, they should be excluded from the feed below.)</p>
<p>My code looks like this:</p>
<pre class="lang-php prettyprint-override"><code>$excluded = implode(',',$entry-&gt;featuredMedia-&gt;ids());
$entries = Entry::find()-&gt;section('blog');
$entries-&gt;id(['not', $excluded);
</code></pre>
<p>Which results in an error <code>&quot;Invalid numeric value: 1602,1630&quot;</code> (The IDs of my excluded entries)</p>
<p>Strangely enough, this example works:</p>
<pre class="lang-php prettyprint-override"><code>$excluded = $entry-&gt;featuredMedia-&gt;ids();
$entries = Entry::find()-&gt;section('blog');
$entries-&gt;id(['not', $excluded[0], $excluded[1]]);
</code></pre>
<p>I know this has something to do with implode() returning a string instead of numeric values, but I can't figure out how to get the right format.</p>

<php><elementquery>
Excluding entries in an entry query with an array of IDs (PHP)
# Excluding entries in an entry query with an array of IDs (PHP)

(Asked by: username_0 on 2023-06-23)

This seems more like a PHP question than a Craft CMS question, but since it's related to an entry query I'm trying to create I figured someone from the Craft community might know. I'm having trouble creating a query that excludes some IDs that are pulled from another entry field. (It's a blog with a "Featured Entries" field at the top, if there are featured entries, they should be excluded from the feed below.)
My code looks like this:
$excluded = implode(',',$entry->featuredMedia->ids());
$entries = Entry::find()->section('blog');
$entries->id(['not', $excluded);

Which results in an error "Invalid numeric value: 1602,1630" (The IDs of my excluded entries)
Strangely enough, this example works:
$excluded = $entry->featuredMedia->ids();
$entries = Entry::find()->section('blog');
$entries->id(['not', $excluded[0], $excluded[1]]);

I know this has something to do with implode() returning a string instead of numeric values, but I can't figure out how to get the right format.

**Tags:** php, elementquery


---

## 1 answers

### Answer by username_1 on 2023-06-23. Score: 3 | Accepted answer

The implode function will turn the array of IDs into a comma-separated string. Then you're putting that into an array with 'not' as the first element, so the parameter you're passing to id() looks like this:
['not', '1602,1630']

This isn't one of the supported formats – if you want to exclude a list of IDs, you pass an array of IDs with 'not' as the first element:
['not', 1602, 1630]

Instead of using implode, you can use the array returned by your first query as-is, only adding 'not' at the beginning using array_unshift:
$excluded = $entry->featuredMedia->ids();
array_unshift($excluded, 'not');

$entries = Entry::find()
    ->section('blog')
    ->id($excluded)
    ->all();

> **Comments:**
> **username_0**: That worked perfectly and seems obvious in retrospect. Thanks for the help!  

---

