1

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 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.

1 Answer 1

3

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);
5
  • 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. Commented Jan 30, 2015 at 16:20
  • 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
    Commented Jan 30, 2015 at 16:25
  • 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? Commented Jan 30, 2015 at 16:30
  • 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. Commented Jan 30, 2015 at 16:42
  • 1
    Bugger. It's always the small things that cost most of your time ...
    – Paul
    Commented Jan 30, 2015 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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