<p>You'll need to create a new table/record to store the data</p>

<pre><code>class IdRecord extends BaseRecord
{
    /**
     * Returns the name of the database table the model is associated with (sans table prefix). By convention,
     * tables created by plugins should be prefixed with the plugin name and an underscore.
     *
     * @return string
     */
    public function getTableName()
    {
        return 'ids';
    }

    /**
     * Returns an array of attributes which map back to columns in the database table.
     *
     * @access protected
     * @return array
     */
    protected function defineAttributes()
    {
        return array(
            'oldId'   =&gt; AttributeType::Number,
            'newId' =&gt; AttributeType::Number
        );
    }
}
</code></pre>

<p>Create a new Controller and store with the record</p>

<pre><code>public function actionCreateTag()
{
    $this-&gt;requireLogin();
    $this-&gt;requireAjaxRequest();

    $tag = new TagModel();
    $tag-&gt;groupId = craft()-&gt;request-&gt;getRequiredPost('groupId');
    $tag-&gt;getContent()-&gt;title = trim(craft()-&gt;request-&gt;getRequiredPost('title'));
    $oldId = craft()-&gt;request-&gt;getRequiredPost('oldId');

    if (craft()-&gt;tags-&gt;saveTag($tag))
    {

        $tagMap = new IdRecord();
        $tagMap-&gt;oldId = $oldId;
        $tagMap-&gt;newId =  $tag-&gt;id;
        $tagMap-&gt;save();
        $this-&gt;returnJson(array(
            'success' =&gt; true,
            'id'      =&gt; $tag-&gt;id
        ));
    }
    else
    {
        $this-&gt;returnJson(array(
            'success' =&gt; false
        ));
    }
}
</code></pre>

<p>And when you are saving your entries you'll do</p>

<pre><code>$entry = new EntryModel();
// ... set your content
$oldId = 'number of your old WP tag id';
$tag = IdRecord::model()-&gt;find('oldId = :oldId',[
    'oldId' =&gt; $oldId
]);
$entry-&gt;getContent()-&gt;setAttribute('yourTagFieldHandle', $tag-&gt;newId);
</code></pre>

<p>You can use <a href="http://pluginfactory.io" rel="nofollow noreferrer">pluginfactory.io</a> to create your boilerplate.</p>

<h3>Edit</h3>

<p>Another approach. If you receive the title of the tags as an array like <code>['tag1', 'tag2', 'tag3']</code> would be something like</p>

<pre><code>$ids = [];
foreach($tagTitles as $tagTitle){
    $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Tag);
    $criteria-&gt;title = 'your Tag Title';
    $tag = $criteria-&gt;first();
    if($tag){
        $ids[] = $tag-&gt;id;
    }
}
$entry-&gt;getContent()-&gt;setAttribute('yourFieldHandle', $ids);
</code></pre>

CC BY-SA 3.0
2018-03-15T15:09:35.057Z
2018-03-16T11:05:16.430Z
2018-03-16T11:05:16.430Z
Robin Schambach
<p>I'm trying to create an jQuery/Ajax script to import data from a Wordpress API service and want to import the WP tags into my database.</p>

<p>Everything works as expected when I loop through the data and send it to <code>/actions/tags/createTag</code> but I'd like to keep the original tag IDs in order to be able to import the posts later and keep the relationship with their tags (Craft automatically assigns a <em>length+1</em> ID to new tags).</p>

<p>Any way I can do this?</p>

CC BY-SA 3.0
2018-03-14T21:32:14.467Z
Crafts shares all ids for elements so when you create an entry it starts with id 1, when you create a category it has id 2, your tag has id 3 and so on. You won't be able to keep your ids because of that. I would suggest you to save the old/new ids and map them into an array. I could explain you how if you want
Robin Schambach
CC BY-SA 3.0
2018-03-14T21:38:31.393Z
@RobinSchambach That would be great! Thanks
brunouno
CC BY-SA 3.0
2018-03-14T22:27:29.827Z
Do you use Craft 2 or Craft 3?
Robin Schambach
CC BY-SA 3.0
2018-03-15T14:53:09.833Z
Right now I'm using Craft-2.6.2991
brunouno
<ajax><tags><jquery>
Ajax - Create tag with custom ID programatically
# Ajax - Create tag with custom ID programatically

(Asked by: username_0 on 2018-03-14)

I'm trying to create an jQuery/Ajax script to import data from a Wordpress API service and want to import the WP tags into my database.

Everything works as expected when I loop through the data and send it to `/actions/tags/createTag` but I'd like to keep the original tag IDs in order to be able to import the posts later and keep the relationship with their tags (Craft automatically assigns a *length+1* ID to new tags).

Any way I can do this?

**Tags:** ajax, tags, jquery

> **Comments:**
> **username_1**: Crafts shares all ids for elements so when you create an entry it starts with id 1, when you create a category it has id 2, your tag has id 3 and so on. You won't be able to keep your ids because of that. I would suggest you to save the old/new ids and map them into an array. I could explain you how if you want  
> **username_0**: @RobinSchambach That would be great! Thanks  
> **username_1**: Do you use Craft 2 or Craft 3?  
> **username_0**: Right now I'm using Craft-2.6.2991  

---

## 1 answers

### Answer by username_1 on 2018-03-15. Score: 0 | Accepted answer

You'll need to create a new table/record to store the data
class IdRecord extends BaseRecord
{
    /**
     * Returns the name of the database table the model is associated with (sans table prefix). By convention,
     * tables created by plugins should be prefixed with the plugin name and an underscore.
     *
     * @return string
     */
    public function getTableName()
    {
        return 'ids';
    }

    /**
     * Returns an array of attributes which map back to columns in the database table.
     *
     * @access protected
     * @return array
     */
    protected function defineAttributes()
    {
        return array(
            'oldId'   => AttributeType::Number,
            'newId' => AttributeType::Number
        );
    }
}

Create a new Controller and store with the record
public function actionCreateTag()
{
    $this->requireLogin();
    $this->requireAjaxRequest();

    $tag = new TagModel();
    $tag->groupId = craft()->request->getRequiredPost('groupId');
    $tag->getContent()->title = trim(craft()->request->getRequiredPost('title'));
    $oldId = craft()->request->getRequiredPost('oldId');

    if (craft()->tags->saveTag($tag))
    {

        $tagMap = new IdRecord();
        $tagMap->oldId = $oldId;
        $tagMap->newId =  $tag->id;
        $tagMap->save();
        $this->returnJson(array(
            'success' => true,
            'id'      => $tag->id
        ));
    }
    else
    {
        $this->returnJson(array(
            'success' => false
        ));
    }
}

And when you are saving your entries you'll do
$entry = new EntryModel();
// ... set your content
$oldId = 'number of your old WP tag id';
$tag = IdRecord::model()->find('oldId = :oldId',[
    'oldId' => $oldId
]);
$entry->getContent()->setAttribute('yourTagFieldHandle', $tag->newId);

You can use pluginfactory.io to create your boilerplate.
Edit
Another approach. If you receive the title of the tags as an array like ['tag1', 'tag2', 'tag3'] would be something like
$ids = [];
foreach($tagTitles as $tagTitle){
    $criteria = craft()->elements->getCriteria(ElementType::Tag);
    $criteria->title = 'your Tag Title';
    $tag = $criteria->first();
    if($tag){
        $ids[] = $tag->id;
    }
}
$entry->getContent()->setAttribute('yourFieldHandle', $ids);


---

