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?

  • 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 Mar 14 '18 at 21:32
  • @RobinSchambach That would be great! Thanks
    – brunouno
    Mar 14 '18 at 21:38
  • Do you use Craft 2 or Craft 3? Mar 14 '18 at 22:27
  • Right now I'm using Craft-2.6.2991
    – brunouno
    Mar 15 '18 at 14:53

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()

    $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;
            'success' => true,
            'id'      => $tag->id
            '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.


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();
        $ids[] = $tag->id;
$entry->getContent()->setAttribute('yourFieldHandle', $ids);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.