<pre><code>$entry-&gt;setFieldValue('alcoholWip', array_values($categoryIds));
</code></pre>
<p>Forgot to use <code>array_values</code> to pass in the values only, not the key/value pairs.</p>
<p>See, I told you I was going crazy!!</p>

CC BY-SA 4.0
2021-10-02T06:52:11.660Z
Glad you got it sorted. Hi from PDX! 
August Miller
CC BY-SA 4.0
2021-10-01T18:55:15.180Z
2021-10-01T19:01:17.660Z
2021-10-01T19:01:17.660Z
Sean Delaney
<p>Feel like I'm going crazy here...</p>
<p>I've got this snippet of code.</p>
<p>I find my entry based on title key/value.</p>
<p>I find my category ids based on alcohol key/value.</p>
<p>I attempt to set the entry category relation and save.</p>
<p>I get no errors. But nothing is saved either.</p>
<p>I've found other similar questions but nothing is working.</p>
<p>Need a fresh pair of eyes...</p>
<pre><code>    $entriesWithAlcohol = [
                [
                    'title' =&gt; 'Jupiter Hotel + NEXT',
                    'alcohol' =&gt; ['byo', 'byo-with-corkage-fee'],
                ],
            ];
    
            foreach ($entriesWithAlcohol as $entryWithAlcohol) {
                $entry = \craft\elements\Entry::find()-&gt;title(\craft\helpers\Db::escapeParam($entryWithAlcohol['title']))-&gt;one();
    
                if ($entry) {
                    $categoryIds = [];
    
                    foreach ($entryWithAlcohol['alcohol'] as $alcoholValue) {
                        $category = \craft\elements\Category::find()-&gt;slug(\craft\helpers\Db::escapeParam($alcoholValue))-&gt;one();
    
                        if ($category) {
                            $categoryIds[] = $category-&gt;id;
                        }
                    }
    
                    $entry-&gt;setFieldValue('alcoholWip', $categoryIds);
    
                    if (Craft::$app-&gt;elements-&gt;saveElement($entry)) {
                        Craft::info('Entry updated (ID: '.$entry-&gt;id.').');
                    } else {
                        Craft::error('Could not updated the entry (ID: '.$entry-&gt;id.').', LogLevel::Error);
                    }
                }
    
                //Craft::dd($entry-&gt;alcoholWip);
            }
</code></pre>

<categories><entry><saveentry>
Assign category to entry using a plugin
# Assign category to entry using a plugin

(Asked by: username_0 on 2021-10-01)

Feel like I'm going crazy here...
I've got this snippet of code.
I find my entry based on title key/value.
I find my category ids based on alcohol key/value.
I attempt to set the entry category relation and save.
I get no errors. But nothing is saved either.
I've found other similar questions but nothing is working.
Need a fresh pair of eyes...
    $entriesWithAlcohol = [
                [
                    'title' => 'Jupiter Hotel + NEXT',
                    'alcohol' => ['byo', 'byo-with-corkage-fee'],
                ],
            ];
    
            foreach ($entriesWithAlcohol as $entryWithAlcohol) {
                $entry = \craft\elements\Entry::find()->title(\craft\helpers\Db::escapeParam($entryWithAlcohol['title']))->one();
    
                if ($entry) {
                    $categoryIds = [];
    
                    foreach ($entryWithAlcohol['alcohol'] as $alcoholValue) {
                        $category = \craft\elements\Category::find()->slug(\craft\helpers\Db::escapeParam($alcoholValue))->one();
    
                        if ($category) {
                            $categoryIds[] = $category->id;
                        }
                    }
    
                    $entry->setFieldValue('alcoholWip', $categoryIds);
    
                    if (Craft::$app->elements->saveElement($entry)) {
                        Craft::info('Entry updated (ID: '.$entry->id.').');
                    } else {
                        Craft::error('Could not updated the entry (ID: '.$entry->id.').', LogLevel::Error);
                    }
                }
    
                //Craft::dd($entry->alcoholWip);
            }

**Tags:** categories, entry, saveentry


---

## 1 answers

### Answer by username_0 on 2021-10-01. Score: 1 | Accepted answer

$entry->setFieldValue('alcoholWip', array_values($categoryIds));

Forgot to use array_values to pass in the values only, not the key/value pairs.
See, I told you I was going crazy!!

> **Comments:**
> **username_1**: Glad you got it sorted. Hi from PDX!   

---

