2

Let's say I created a section called, "Answers". In a custom plugin, I want to create an entry that should show up under the "Answers" tab in the CP entries page.

  $entry = new EntryModel();
  // How do I say, save this entry into the "Answers" channel?
  $saveIt = craft()->entries->saveEntry($entry);

enter image description here

I have literally been reading Craft docs for the past hour, and have gotten nowhere.

1 Answer 1

3

The EntryModel you have has a sectionId property you can set. i.e.

 $entry = new EntryModel();
 $entry->sectionId = 1;
 $saveIt = craft()->entries->saveEntry($entry);

Note that there are other properties you'll need to set for it to pass validation. So, if $saveIt is false after calling saveEntry($entry), then call $entry->getErrors() to get an array of validation errors to fix.

2
  • Thanks Brad. I was confused how to refer to the section by name, which obviously is not possible. Commented Jan 26, 2018 at 16:38
  • 2
    @DanielZuzevich for future reference if it's really necessary for whatever reason to set something by handle (not name, the name is not unique) instead of the ID you could do $entry->sectionId = craft()->sections->getSectionByHandle('handle')->id Commented Jan 26, 2018 at 21:09

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.