<p>Like other relational fields, Tags fields’ values need to be set to an array of the tags’ IDs.</p>

<p>It’s not possible to create tags <em>during</em> entry creation; you would just need to create it first using <a href="http://buildwithcraft.com/classreference/services/TagsService#saveTag-detail" rel="nofollow noreferrer">TagsService::saveTag()</a>.</p>

<pre class="lang-php prettyprint-override"><code>$tag = new TagModel();
$tag-&gt;groupId = 1;
$tag-&gt;getContent()-&gt;title = 'My Tag';

$success = craft()-&gt;tags-&gt;saveTag($tag);

if ($success)
{
    $tagData = array($tag-&gt;id);
}
else
{
    $tagData = null;
}

// ...

$entry-&gt;setContentFromPost(array(
    // ...
    'myTagsField' =&gt; $tagData,
));

// ...
</code></pre>

<p>(Note that I’m setting the field value using <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#setContentFromPost-detail" rel="nofollow noreferrer">setContentFromPost()</a> instead of directly on the ContentModel. <a href="https://craftcms.stackexchange.com/questions/4486/what-is-getcontent-the-contentmodel-and-how-do-i-use-it-to-get-and-set-cust">This is the recommended way to set your custom field values</a>, although for relational fields it doesn’t really matter either way.)</p>

CC BY-SA 3.0
2015-01-30T05:56:19.703Z
Brandon after they are created how do you save tags within "craft()->entries->saveEntry($entry)"? Not sure I understand how setContentFromPost works if it's not a post call?
Adam McCombs
CC BY-SA 3.0
2015-01-30T14:10:38.107Z
@AdamMcCombs `setContentFromPost()` is probably just not the best name for the function. 99% of the time it is called, the data passed in is plucked from $_POST, but that’s not the case here.
Brandon Kelly
CC BY-SA 3.0
2015-01-30T16:01:41.787Z
Thanks Brandon. I think that's what's adding to my confusion because it sounds like setContentFromPost and ->getContent() function the same. getContent (instead of say setContent()) reads funny to me but I know I'm being picky about semantics.
Adam McCombs
CC BY-SA 3.0
2014-12-19T11:22:50.620Z
2014-12-19T11:22:50.620Z
2017-04-13T12:51:00.200Z
Brandon Kelly
<p>I'm working on a custom import plugin which needs to import a load of content to fields including categories and tags. Categories is working fine using a comma separated list of category ids:</p>

<pre><code>        $entry = new EntryModel();
        $entry-&gt;authorId  = $eventObject-&gt;author;
        $entry-&gt;enabled   = true;
        $entry-&gt;postDate   = $eventObject-&gt;postDate;
        $entry-&gt;getContent()-&gt;title = $eventObject-&gt;title;
        $entry-&gt;getContent()-&gt;setAttributes(array(
            'slug' =&gt; $eventObject-&gt;slug,
            'locationText' =&gt; $eventObject-&gt;locationText,
            'date' =&gt; $eventObject-&gt;eventDate,
            'overview' =&gt; $eventObject-&gt;overview,
            'eventWebsite' =&gt; $eventObject-&gt;eventWebsite,
            'eventCategories' =&gt; $eventObject-&gt;categories,
            'location' =&gt; $eventObject-&gt;location,
            'eventTags' =&gt; 1453,
        ));
</code></pre>

<p>I just can't get tags importing at all, also need to create new tags where a tag doesn't already exist! Testing with the id 1453which is an existing tag id.</p>

<plugin-development><tags>
How do you add tags to tag field when saving an entry?
# How do you add tags to tag field when saving an entry?

(Asked by: username_0 on 2014-12-18)

I'm working on a custom import plugin which needs to import a load of content to fields including categories and tags. Categories is working fine using a comma separated list of category ids:
        $entry = new EntryModel();
        $entry->authorId  = $eventObject->author;
        $entry->enabled   = true;
        $entry->postDate   = $eventObject->postDate;
        $entry->getContent()->title = $eventObject->title;
        $entry->getContent()->setAttributes(array(
            'slug' => $eventObject->slug,
            'locationText' => $eventObject->locationText,
            'date' => $eventObject->eventDate,
            'overview' => $eventObject->overview,
            'eventWebsite' => $eventObject->eventWebsite,
            'eventCategories' => $eventObject->categories,
            'location' => $eventObject->location,
            'eventTags' => 1453,
        ));

I just can't get tags importing at all, also need to create new tags where a tag doesn't already exist! Testing with the id 1453which is an existing tag id.

**Tags:** plugin-development, tags

---

## 1 answers

### Answer by username_1 on 2014-12-19. Score: 3

Like other relational fields, Tags fields’ values need to be set to an array of the tags’ IDs.
It’s not possible to create tags during entry creation; you would just need to create it first using TagsService::saveTag().
$tag = new TagModel();
$tag->groupId = 1;
$tag->getContent()->title = 'My Tag';

$success = craft()->tags->saveTag($tag);

if ($success)
{
    $tagData = array($tag->id);
}
else
{
    $tagData = null;
}

// ...

$entry->setContentFromPost(array(
    // ...
    'myTagsField' => $tagData,
));

// ...

(Note that I’m setting the field value using setContentFromPost() instead of directly on the ContentModel. This is the recommended way to set your custom field values, although for relational fields it doesn’t really matter either way.)

> **Comments:**
> **username_2**: Brandon after they are created how do you save tags within "craft()->entries->saveEntry($entry)"? Not sure I understand how setContentFromPost works if it's not a post call?  
> **username_1**: @AdamMcCombs `setContentFromPost()` is probably just not the best name for the function. 99% of the time it is called, the data passed in is plucked from $_POST, but that’s not the case here.  
> **username_2**: Thanks Brandon. I think that's what's adding to my confusion because it sounds like setContentFromPost and ->getContent() function the same. getContent (instead of say setContent()) reads funny to me but I know I'm being picky about semantics.  

---

