Answers.Body: <p>You’d start by creating a new <a href="http://buildwithcraft.com/docs/templating/entrymodel" rel="nofollow noreferrer">EntryModel</a> and copying over the normal properties:</p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>$oldEntry = craft()-&gt;entries-&gt;getEntryById(100);
  
  $newEntry = new EntryModel();
  
  // core element attributes
  $newEntry-&gt;enabled = $oldEntry-&gt;enabled;
  $newEntry-&gt;locale = $oldEntry-&gt;locale;
  $newEntry-&gt;localeEnabled = $oldEntry-&gt;localeEnabled;
  $newEntry-&gt;slug = $oldEntry-&gt;slug;
  $newEntry-&gt;uri = $oldEntry-&gt;uri;
  $newEntry-&gt;getContent()-&gt;title = $oldEntry-&gt;getContent()-&gt;title;
  
  // entry attributes
  $newEntry-&gt;sectionId = $oldEntry-&gt;sectionId;
  $newEntry-&gt;typeId = $oldEntry-&gt;typeId;
  $newEntry-&gt;authorId = $oldEntry-&gt;authorId;
  $newEntry-&gt;postDate = $oldEntry-&gt;postDate;
  $newEntry-&gt;expiryDate = $oldEntry-&gt;expiryDate;
  
  // do this if it's in a Structure section
  if (($parent = $oldEntry-&gt;getParent()) !== null)
  {
      $newEntry-&gt;parentId = $parent-&gt;id;
  }
  </code></pre>
  
  <p>Where things get tricky is copying all of the content. That's tough because many field types expect their values to be in one format when saving, but they return their values in another format when you call <code>$entry-&gt;myFieldHandle</code>. For example relationship fields (Entries, Assets, Users, Categories, and Tags) each expect you to set their values as an array of related elements’ IDs when saving an entry, but when you call <code>$entry-&gt;myRelationalField</code> (or <code>entry.myRelationalField</code> from a template) you’re getting an <a href="http://buildwithcraft.com/classreference/models/ElementCriteriaModel" rel="nofollow noreferrer">ElementCriteriaModel</a> back. So for those, you’d have to do this:</p>
  
  <pre class="lang-php prettyprint-override"><code>$newEntry-&gt;setContentFromPost(array(
      'myAssetsField' =&gt; $oldEntry-&gt;myAssetsField-&gt;ids()
  ));
  </code></pre>
  
  <p>There are some other posts that touch on how to format the various field types’ values (i.e. <a href="https://craftcms.stackexchange.com/q/8517/9">Duplicating Matrix fields with content from another locale</a>), but nothing comprehensive.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-17T11:45:03.903Z
Answers.LastActivityDate: 2015-10-17T16:04:07.790Z
Answers.LastEditDate: 2017-04-13T12:51:00.253Z
Answers.UserDisplayName: Brandon Kelly
Answers.Body: <p><code>$clonedEntry = Craft::$app-&gt;getElements()-&gt;duplicateElement($entry);</code></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-08T20:37:52.120Z
Answers.LastActivityDate: 2022-04-08T20:37:52.120Z
Answers.UserDisplayName: Brimby
Body: <p>How can I create a plugin that will let me copy entry?</p>
  
  <p>I found this <a href="https://craftcms.stackexchange.com/questions/1651/how-to-duplicate-entries">question</a> but don't have a good answer.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-10-16T22:49:15.587Z
Comments.Text: You specifically want to do it programmatically from a plugin and not from the control panel, correct?
Comments.UserDisplayName: Brad Bell
Tags: <entry>
Title: How can I copy an entry?
ThreadText: # How can I copy an entry?
  
  (Asked by: username_0 on 2015-10-16)
  
  How can I create a plugin that will let me copy entry?
  
  I found this [question](https://craftcms.stackexchange.com/questions/1651/how-to-duplicate-entries) but don't have a good answer.
  
  **Tags:** entry
  
  > **Comments:**
  > **username_1**: You specifically want to do it programmatically from a plugin and not from the control panel, correct?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-10-17. Score: 3
  
  You’d start by creating a new [EntryModel](http://buildwithcraft.com/docs/templating/entrymodel) and copying over the normal properties:
  
  ```php
  $oldEntry = craft()->entries->getEntryById(100);
  
  $newEntry = new EntryModel();
  
  // core element attributes
  $newEntry->enabled = $oldEntry->enabled;
  $newEntry->locale = $oldEntry->locale;
  $newEntry->localeEnabled = $oldEntry->localeEnabled;
  $newEntry->slug = $oldEntry->slug;
  $newEntry->uri = $oldEntry->uri;
  $newEntry->getContent()->title = $oldEntry->getContent()->title;
  
  // entry attributes
  $newEntry->sectionId = $oldEntry->sectionId;
  $newEntry->typeId = $oldEntry->typeId;
  $newEntry->authorId = $oldEntry->authorId;
  $newEntry->postDate = $oldEntry->postDate;
  $newEntry->expiryDate = $oldEntry->expiryDate;
  
  // do this if it's in a Structure section
  if (($parent = $oldEntry->getParent()) !== null)
  {
      $newEntry->parentId = $parent->id;
  }
  
  ```
  
  Where things get tricky is copying all of the content. That's tough because many field types expect their values to be in one format when saving, but they return their values in another format when you call `$entry->myFieldHandle`. For example relationship fields (Entries, Assets, Users, Categories, and Tags) each expect you to set their values as an array of related elements’ IDs when saving an entry, but when you call `$entry->myRelationalField` (or `entry.myRelationalField` from a template) you’re getting an [ElementCriteriaModel](http://buildwithcraft.com/classreference/models/ElementCriteriaModel) back. So for those, you’d have to do this:
  
  ```php
  $newEntry->setContentFromPost(array(
      'myAssetsField' => $oldEntry->myAssetsField->ids()
  ));
  
  ```
  
  There are some other posts that touch on how to format the various field types’ values (i.e. [Duplicating Matrix fields with content from another locale](https://craftcms.stackexchange.com/q/8517/9)), but nothing comprehensive.
  
  
  ---
  
  ### Answer by username_3 on 2022-04-08. Score: 2
  
  `$clonedEntry = Craft::$app->getElements()->duplicateElement($entry);`
  
  
  ---
  
  