<p>To relate the tags, all you'd have to do is feed the entry's tag field an array containing tag IDs.</p>

<p>So, after creating (or finding existing) tags, you should end up with an array with one or more tag IDs.</p>

<p>Then, if it's a new entry, you would do:</p>

<pre><code>$myTagIds = array(1,2,3,4); // your tag IDs
$entry = new EntryModel();
$entry-&gt;setContentFromPost(array(
    // ...
    'tagFieldHandle' =&gt; $myTagIds,
));

craft()-&gt;entries-&gt;saveEntry($entry);
</code></pre>

CC BY-SA 3.0
2015-01-30T16:20:53.577Z
Paul - I think your missing a closing parenthesis on line 6 but either way, I can't get setContentFromPost to save my tag array, it saves the entry but the tag field is blank. I can see the array in the craft_entryversions but the tag field (when I go to edit the entry) is blank. I've double checked for valid tag ID's too.
Adam McCombs
CC BY-SA 3.0
2015-01-30T16:25:38.177Z
I always look for answers in Craft's controllers, but I assume you did that already. Don't have time to get into this now. Could it be a locale issue?
Paul
CC BY-SA 3.0
2015-01-30T16:30:29.383Z
Only one locale here and now I'm looking into the craft controller. I updated my original post to show the snippet I'm using. Perhaps I'm missing something?
Adam McCombs
CC BY-SA 3.0
2015-01-30T16:42:16.667Z
I got it. The tag group wasn't specified in the field settings so even though I was specifying the tag IDs I guess it couldn't find them in it's selected group.
Adam McCombs
CC BY-SA 3.0
2015-01-30T16:43:36.743Z
Bugger. It's always the small things that cost most of your time ...
Paul
CC BY-SA 3.0
2015-01-30T13:01:52.110Z
2015-01-30T16:22:12.553Z
2015-01-30T16:22:12.553Z
Paul
<p>As best I can tell entries have to be created, tags have to be created, a relationship created and then an update to the original entry. Is this correct or is there a simpler way to do this?</p>

<p>There is a similar question <a href="https://craftcms.stackexchange.com/questions/5327/how-do-you-add-tags-to-tag-field-when-saving-an-entry">here</a> which address how to create a tags but I feel like how to relate the new tag(s) to the entry could use some elaboration.</p>

<tags><import>
Best way to create tags under new EntryModel?
# Best way to create tags under new EntryModel?

(Asked by: username_0 on 2015-01-30)

As best I can tell entries have to be created, tags have to be created, a relationship created and then an update to the original entry. Is this correct or is there a simpler way to do this?

There is a similar question [here](https://craftcms.stackexchange.com/questions/5327/how-do-you-add-tags-to-tag-field-when-saving-an-entry) which address how to create a tags but I feel like how to relate the new tag(s) to the entry could use some elaboration.

**Tags:** tags, import

---

## 1 answers

### Answer by username_1 on 2015-01-30. Score: 3 | Accepted answer

To relate the tags, all you'd have to do is feed the entry's tag field an array containing tag IDs.
So, after creating (or finding existing) tags, you should end up with an array with one or more tag IDs.
Then, if it's a new entry, you would do:
$myTagIds = array(1,2,3,4); // your tag IDs
$entry = new EntryModel();
$entry->setContentFromPost(array(
    // ...
    'tagFieldHandle' => $myTagIds,
));

craft()->entries->saveEntry($entry);

> **Comments:**
> **username_0**: Paul - I think your missing a closing parenthesis on line 6 but either way, I can't get setContentFromPost to save my tag array, it saves the entry but the tag field is blank. I can see the array in the craft_entryversions but the tag field (when I go to edit the entry) is blank. I've double checked for valid tag ID's too.  
> **username_1**: I always look for answers in Craft's controllers, but I assume you did that already. Don't have time to get into this now. Could it be a locale issue?  
> **username_0**: Only one locale here and now I'm looking into the craft controller. I updated my original post to show the snippet I'm using. Perhaps I'm missing something?  
> **username_0**: I got it. The tag group wasn't specified in the field settings so even though I was specifying the tag IDs I guess it couldn't find them in it's selected group.  
> **username_1**: Bugger. It's always the small things that cost most of your time ...  

---

