Skip to content

Instantly share code, notes, and snippets.

@DZuz14
Created June 22, 2018 01:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DZuz14/b54b7266b0db39467bf265e0cec4559e to your computer and use it in GitHub Desktop.
Save DZuz14/b54b7266b0db39467bf265e0cec4559e to your computer and use it in GitHub Desktop.
Save Craft 3 Entry With PHP
<?php
$section = Craft::$app->sections->getSectionByHandle('journalArticles');
$entryTypes = $section->getEntryTypes();
$entryType = reset($entryTypes);
$entry = new Entry([
'sectionId' => $section->id,
'typeId' => $entryType->id,
'fieldLayoutId' => $entryType->fieldLayoutId,
'authorId' => 1,
'title' => 'My Entry',
'slug' => 'my-entry',
'postDate' => new DateTime(),
]);
$entry->setFieldValues([
'journalTitle' => 'Hello'
]);
$success = Craft::$app->elements->saveElement($entry);
if(!$success) {
echo "why";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment