Answers.Body: <p>Something like this should work. I've also cleaned up the code a bit to be correct PHP syntax as my assumption is that you''ll be doing this in a module/plugin.</p>
  
  <pre><code>// Get all people entries
  $entries = Entry::find()-&gt;section('people')-&gt;all();
  
  foreach ($entries as $entry) {
      // If this was a "featured" historical person
      if ($entry-&gt;historical == 'featured') {
          // Change to featuredPeople entry type
          $entry-&gt;typeId = $featuredPeopleEntryTypeId;
  
          // Save entry
          Craft::$app-&gt;elements-&gt;saveElement($entry);
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-21T22:08:08.863Z
Answers.Text: Hi Ben. That was pseudocode as the historical featured check is a complicated piece of logic on its own. However, just changing the typeId and re-saving does appear to have worked. The field data appears to be intact. I super didn't expect it to be that easy. Thanks.
Answers.UserDisplayName: JamesNZ
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-22T08:04:27.550Z
Answers.Text: Ah I see. Yes, it's that simple :)
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-21T08:45:19.590Z
Answers.LastActivityDate: 2018-11-21T08:45:19.590Z
Answers.UserDisplayName: Ben Croker
Answers.Body: <p>For future reference of anybody stumbling onto this post (like me), setting -&gt;typeId works for like 90%, but to cover it all put this in it too:</p>
  <pre><code>$entry-&gt;fieldLayoutId = null;
  </code></pre>
  <p>If you don't throw the field layout out, it might still hold on to the old fields of the entry. This gives some really weird issues, like a relational field being filled in the cp, but in twig the field doesn't exist</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-27T12:41:33.280Z
Answers.LastActivityDate: 2021-02-27T12:41:33.280Z
Answers.UserDisplayName: Bastiaan Wuisman
Body: <p>I have a <strong>people channel</strong> that historically has used another field to decide if they're a "featured" person or not. </p>
  
  <p>The field set for "<strong>featured</strong>" people has now diverged from what a normal person has, so I'm wanting to create a new entry type in the people channel (<strong>featuredPeople</strong>) and programatically shift these people to the new entry type.</p>
  
  <p>The field set is additive to the old one, so I'm hoping due to there being no field/structural changes this isn't too hard?</p>
  
  <p>I've got the bones of a script together to do this, but I'm struggling to find any info on how to actually change the entry type on the entry.</p>
  
  <pre class="lang-php prettyprint-override"><code># Get all people
  $peopleCrit = Entry::find();
  $peopleCrit-&gt;section = "people";
  $people = $peopleCrit-&gt;all() ?? [];
  
  foreach ($people as $person) {
  
      # If this was a "featured" person, shift to new featuredPeople entry type.
      if (historical "is featured" check) {
  
          # Move to featuredPeople entry type
          ???
  
      }
  
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
Tags: <plugin-development><entries><craft3>
Title: Programatically Changing Entry Types?
ThreadText: # Programatically Changing Entry Types?
  
  (Asked by: username_0 on 2018-11-19)
  
  I have a **people channel** that historically has used another field to decide if they're a "featured" person or not.
  
  The field set for "**featured**" people has now diverged from what a normal person has, so I'm wanting to create a new entry type in the people channel (**featuredPeople**) and programatically shift these people to the new entry type.
  
  The field set is additive to the old one, so I'm hoping due to there being no field/structural changes this isn't too hard?
  
  I've got the bones of a script together to do this, but I'm struggling to find any info on how to actually change the entry type on the entry.
  
  ```php
  # Get all people
  $peopleCrit = Entry::find();
  $peopleCrit->section = "people";
  $people = $peopleCrit->all() ?? [];
  
  foreach ($people as $person) {
  
      # If this was a "featured" person, shift to new featuredPeople entry type.
      if (historical "is featured" check) {
  
          # Move to featuredPeople entry type
          ???
  
      }
  
  }
  
  ```
  
  Any help would be greatly appreciated.
  
  **Tags:** plugin-development, entries, craft3
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-11-21. Score: 3 | Accepted answer
  
  Something like this should work. I've also cleaned up the code a bit to be correct PHP syntax as my assumption is that you''ll be doing this in a module/plugin.
  
  ```
  // Get all people entries
  $entries = Entry::find()->section('people')->all();
  
  foreach ($entries as $entry) {
      // If this was a "featured" historical person
      if ($entry->historical == 'featured') {
          // Change to featuredPeople entry type
          $entry->typeId = $featuredPeopleEntryTypeId;
  
          // Save entry
          Craft::$app->elements->saveElement($entry);
      }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Hi Ben. That was pseudocode as the historical featured check is a complicated piece of logic on its own. However, just changing the typeId and re-saving does appear to have worked. The field data appears to be intact. I super didn't expect it to be that easy. Thanks.  
  > **username_1**: Ah I see. Yes, it's that simple :)  
  
  ---
  
  ### Answer by username_2 on 2021-02-27. Score: 2
  
  For future reference of anybody stumbling onto this post (like me), setting ->typeId works for like 90%, but to cover it all put this in it too:
  
  ```
  $entry->fieldLayoutId = null;
  
  ```
  
  If you don't throw the field layout out, it might still hold on to the old fields of the entry. This gives some really weird issues, like a relational field being filled in the cp, but in twig the field doesn't exist
  
  
  ---
  
  