<p>Tags are normal Elements like <code>Entries</code>, <code>Categories</code>, <code>Users</code> and so on.. In order to relate a <code>Tag</code> to another element, you need to insert the array of those elements like you are used to with every other field</p>

<pre><code>$existingTag = Tag::find()-&gt;title('whatever')-&gt;one();

$tag = new Tag();
$tag-&gt;groupId = 1; //&lt;-- insert your group id here
$tag-&gt;title = "something";
Craft::$app-&gt;getElements()-&gt;saveElement($tag);

$entry-&gt;setFieldValues([
   'tags' =&gt; [$tag-&gt;id, $existingTag-&gt;id]
]);
</code></pre>

CC BY-SA 4.0
2018-06-22T16:22:49.407Z
This worked. Its unfortunate that I need to use the id of the tag, and cant just use the handle. Do they use the id because there is a possibility of name collisions across different groups of existing tags?
Dan Zuzevich
CC BY-SA 4.0
2018-06-22T18:49:27.880Z
Yes that's the case. The title is not unique so the same title can exists in multiple groups but since one tag field is always linked to a certain group you would be able to match the correct one anyway. The more important reason is to keep things consistent. All relation fields use the ID, why should the tag field use something different? It's much more intuitive and logical (if you are used to Craft and know how things work)
Robin Schambach
CC BY-SA 4.0
2018-06-23T13:10:32.830Z
Still working on learning all of the craft backend internals. Getting better, but I still have a lot to learn. Makes sense.
Dan Zuzevich
CC BY-SA 4.0
2018-06-22T15:34:10.033Z
2018-06-22T15:34:10.033Z
Robin Schambach
<p>I am able to successfully create new entries with PHP when I am using basic field values that are Rich Text or Plain Text fields, but am unsure how to add Tags to an entry.</p>

<p>So my entry already has a field called 'tags' on it, and I thought that maybe it wanted an array of strings like so:</p>

<pre><code>$entry-&gt;setFieldValues([
   'tags' =&gt; array('someCoolTag', 'tagasaurus rex')
]);
</code></pre>

<p>I admit I do not really understand how tags work behind the scenes, and would like to correct that. Any help is great.</p>

<craft3><tags>
Add Tags to a Newly Created Entry With PHP in Craft 3
# Add Tags to a Newly Created Entry With PHP in Craft 3

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

I am able to successfully create new entries with PHP when I am using basic field values that are Rich Text or Plain Text fields, but am unsure how to add Tags to an entry.
So my entry already has a field called 'tags' on it, and I thought that maybe it wanted an array of strings like so:
$entry->setFieldValues([
   'tags' => array('someCoolTag', 'tagasaurus rex')
]);

I admit I do not really understand how tags work behind the scenes, and would like to correct that. Any help is great.

**Tags:** craft3, tags

---

## 1 answers

### Answer by username_1 on 2018-06-22. Score: 2 | Accepted answer

Tags are normal Elements like Entries, Categories, Users and so on.. In order to relate a Tag to another element, you need to insert the array of those elements like you are used to with every other field
$existingTag = Tag::find()->title('whatever')->one();

$tag = new Tag();
$tag->groupId = 1; //<-- insert your group id here
$tag->title = "something";
Craft::$app->getElements()->saveElement($tag);

$entry->setFieldValues([
   'tags' => [$tag->id, $existingTag->id]
]);

> **Comments:**
> **username_0**: This worked. Its unfortunate that I need to use the id of the tag, and cant just use the handle. Do they use the id because there is a possibility of name collisions across different groups of existing tags?  
> **username_1**: Yes that's the case. The title is not unique so the same title can exists in multiple groups but since one tag field is always linked to a certain group you would be able to match the correct one anyway. The more important reason is to keep things consistent. All relation fields use the ID, why should the tag field use something different? It's much more intuitive and logical (if you are used to Craft and know how things work)  
> **username_0**: Still working on learning all of the craft backend internals. Getting better, but I still have a lot to learn. Makes sense.  

---

