Answers.Body: <p>That’s a little more complicated, and at that point you’re going to be best off writing your own PHP code to do it.</p>
  
  <p>Craft has a bootstrap file that makes it very easy to write custom PHP code without going to the trouble of writing a plugin, which is great for one-off tasks like this.</p>
  
  <p>Just create a new file alongside your index.php file, called move_entry.php. Here’s what it should look like:</p>
  
  
  
  <h2>Craft 3</h2>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  
  // ...
  // copy contents of index.php here, except for the last `$app-&gt;run();` line
  // ...
  
  
  // Start a transaction in case anything goes south
  $transaction = $app-&gt;db-&gt;beginTransaction();
  try {
      // Fetch the entry you need to move
      $entry = \craft\elements\Entry::findOne(184);
  
      // Remove it from its old structure
      $oldSection = $entry-&gt;getSection();
  
      $record = \craft\records\StructureElement::findOne([
          'structureId' =&gt; $oldSection-&gt;structureId,
          'elementId' =&gt; $entry-&gt;id
      ]);
  
      if (!$record-&gt;deleteWithChildren()) {
          throw new \Exception('Could not delete the old structure node');
      }
  
      // Set the new sectionId and typeId on the entry
      $entry-&gt;sectionId = 2;
      $entry-&gt;typeId = 2;
  
      // Append it to the end of the new structure
      $newSection = $entry-&gt;getSection();
  
      if (!$app-&gt;structures-&gt;appendToRoot($newSection-&gt;structureId, $entry, 'insert')) {
          throw new \Exception('Could not insert the new structure node');
      }
  
      // Save the entry
      if (!$app-&gt;elements-&gt;saveElement($entry)) {
          throw new \Exception('Could not save the entry');
      }
  
      // Commit the transaction
      $transaction-&gt;commit();
  } catch (\Exception $e) {
      // Undo whatever changes we already made
      $transaction-&gt;rollBack();
      throw $e;
  }
  
  echo 'success!';
  </code></pre>
  
  <h2>Craft 2</h2>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  namespace Craft;
  
  // Initialize Craft without letting it take over the whole request
  $app = require '../craft/app/bootstrap.php';
  
  // Set Craft to Craft Personal so we don't get any errors about Client/Pro-only components not being available
  $app-&gt;getInfo()-&gt;edition = 0;
  
  // Start a transaction in case anything goes south
  $transaction = $app-&gt;db-&gt;beginTransaction();
  try
  {
      // Fetch the entry you need to move
      $entry = $app-&gt;entries-&gt;getEntryById(184);
  
      // Remove it from its old structure
      $oldSection = $entry-&gt;getSection();
  
      $record = StructureElementRecord::model()-&gt;findByAttributes(array(
          'structureId' =&gt; $oldSection-&gt;structureId,
          'elementId'   =&gt; $entry-&gt;id
      ));
  
      if (!$record-&gt;deleteNode())
      {
          throw new Exception('Could not delete the old structure node');
      }
  
      // Set the new sectionId and typeId on the entry
      $entry-&gt;sectionId = 2;
      $entry-&gt;typeId = 2;
  
      // Append it to the end of the new structure
      $newSection = $entry-&gt;getSection();
  
      if (!$app-&gt;structures-&gt;appendToRoot($newSection-&gt;structureId, $entry, 'insert'))
      {
          throw new Exception('Could not insert the new structure node');
      }
  
      // Save the entry
      if (!$app-&gt;entries-&gt;saveEntry($entry))
      {
          throw new Exception('Could not save the entry');
      }
  
      // Commit the transaction
      $transaction-&gt;commit();
  
      echo 'success!';
  }
  catch (\Exception $e)
  {
      // Undo whatever changes we already made
      $transaction-&gt;rollback();
  
      throw $e;
  }
  </code></pre>
  
  <p>Plug the right entry/section/type IDs in there, point your browser to <a href="http://example.com/move_entry.php" rel="nofollow noreferrer">http://example.com/move_entry.php</a> and you should be all set!</p>
  
  <p><strong>Warning:</strong> Make sure you never run this code for an entry that has any children in the structure, as its children will end up getting removed from the hierarchy.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-31T09:18:29.933Z
Answers.Text: Thanks for the detailed answer Brandon! But unfortunately I am getting the success message but the entry hasn't moved into the new structure.
Answers.UserDisplayName: Jamie Wade
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-31T12:03:23.393Z
Answers.Text: @JamieWade I had tested locally and it worked. Are you sure you put all the right IDs in there? If so, maybe just send a DB backup over to support@buildwithcraft.com
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-31T13:03:19.223Z
Answers.Text: Sorry Brandon, I've just run another test through and it seems to have worked fine and parenting is working as well, so thanks for that and sorry for taking up your time on this! One little thing however, if I run this on a parent entry, and then run it again on one of it's child entries (which have been moved to top level because the parent has gone), I get: "Fatal error: Call to a member function deleteNode() on a non-object in /vagrant/public/move_entry.php on line 25" Just a little thing as I will remember to move child entries first, but otherwise it's all good so thanks again.
Answers.UserDisplayName: Jamie Wade
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-31T13:27:44.310Z
Answers.Text: @JamieWade Ah yes, didn’t think about that. Calling `deleteNode()` will remove that entry and all of its decedents from the hierarchy, so yeah, only do this with entries that don’t have any children.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-29T03:44:33.057Z
Answers.Text: Is it possible to move an Entry and it's children?
Answers.UserDisplayName: julzmon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-27T19:31:45.133Z
Answers.Text: @julzmon your best bet would just be to move them one at a time, and re-organize them once they're all moved.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-21T08:39:54.843Z
Answers.Text: Its worth noting, you can actually move a parent and its children in one go, but when they are moved, you'll lose the hierarchy structure, so you still have to replicate it, but you can pass an array on the ids and entry types.
Answers.UserDisplayName: James White
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-06-02T11:20:11.210Z
Answers.Text: Is this still good for Craft 3.6.15 and in a multi-site ?
  Move entry to another section in another site ?
Answers.UserDisplayName: Popaweb
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-14T21:19:13.437Z
Answers.Text: How to do this in Craft CMS 4? When I follow these instructions, I get a 404 when trying to call the php-file (also after changing the htaccess).
Answers.UserDisplayName: Mitchell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-14T21:47:19.543Z
Answers.Text: I managed to run the script through another way in Craft CMS 4, but it doesn't seem to work with Craft CMS 4. I'd appreciate if someone could make a Craft CMS 4-version of this script, since I have many complex entries to move in multiple languages (so too much manual work) and I don't know php.
Answers.UserDisplayName: Mitchell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2014-10-30T21:26:41.340Z
Answers.LastActivityDate: 2019-01-19T13:15:32.953Z
Answers.LastEditDate: 2019-01-19T13:15:32.953Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>Seeing as moving entries between sections isn't supported (yet) through the Craft interface, I followed <a href="https://craftcms.stackexchange.com/questions/2021/move-entries-between-sections">Brandon's answer</a> for a workaround through the database.</p>
  
  <p>I am moving entries from one structure to another; updating the <code>sectionId</code> in the database works fine and the entries are moving over without a problem.</p>
  
  <p>However the issues start when I try and make some of the entries children to others that have also been moved.</p>
  
  <p>I have tried both dragging the entry through the Structure view and setting the parent in the dropdown while editing the entry, but both are coming up with the same error:</p>
  
  <blockquote>
    <p>An unknown error occurred.</p>
  </blockquote>
  
  <p>And I am getting a 500 Internal Server error on this file</p>
  
  <pre><code>/index.php?p=admincp/api/structures/moveElement 500 (Internal Server Error)
  </code></pre>
  
  <p>Is this a bug within Craft, or have I missed out on some other information that needs to be updated in the database?</p>
  
Tags: <entries><structure><database>
Title: Move entry from one structure to another with parenting
ThreadText: # Move entry from one structure to another with parenting
  
  (Asked by: username_0 on 2014-10-29)
  
  Seeing as moving entries between sections isn't supported (yet) through the Craft interface, I followed [username_1's answer](https://craftcms.stackexchange.com/questions/2021/move-entries-between-sections) for a workaround through the database.
  
  I am moving entries from one structure to another; updating the `sectionId` in the database works fine and the entries are moving over without a problem.
  
  However the issues start when I try and make some of the entries children to others that have also been moved.
  
  I have tried both dragging the entry through the Structure view and setting the parent in the dropdown while editing the entry, but both are coming up with the same error:
  
  > An unknown error occurred.
  
  And I am getting a 500 Internal Server error on this file
  
  ```
  /index.php?p=admincp/api/structures/moveElement 500 (Internal Server Error)
  
  ```
  
  Is this a bug within Craft, or have I missed out on some other information that needs to be updated in the database?
  
  **Tags:** entries, structure, database
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-10-30. Score: 8 | Accepted answer
  
  That’s a little more complicated, and at that point you’re going to be best off writing your own PHP code to do it.
  
  Craft has a bootstrap file that makes it very easy to write custom PHP code without going to the trouble of writing a plugin, which is great for one-off tasks like this.
  
  Just create a new file alongside your index.php file, called move\_entry.php. Here’s what it should look like:
  
  ## Craft 3
  
  ```php
  <?php
  
  // ...
  // copy contents of index.php here, except for the last `$app->run();` line
  // ...
  
  // Start a transaction in case anything goes south
  $transaction = $app->db->beginTransaction();
  try {
      // Fetch the entry you need to move
      $entry = \craft\elements\Entry::findOne(184);
  
      // Remove it from its old structure
      $oldSection = $entry->getSection();
  
      $record = \craft\records\StructureElement::findOne([
          'structureId' => $oldSection->structureId,
          'elementId' => $entry->id
      ]);
  
      if (!$record->deleteWithChildren()) {
          throw new \Exception('Could not delete the old structure node');
      }
  
      // Set the new sectionId and typeId on the entry
      $entry->sectionId = 2;
      $entry->typeId = 2;
  
      // Append it to the end of the new structure
      $newSection = $entry->getSection();
  
      if (!$app->structures->appendToRoot($newSection->structureId, $entry, 'insert')) {
          throw new \Exception('Could not insert the new structure node');
      }
  
      // Save the entry
      if (!$app->elements->saveElement($entry)) {
          throw new \Exception('Could not save the entry');
      }
  
      // Commit the transaction
      $transaction->commit();
  } catch (\Exception $e) {
      // Undo whatever changes we already made
      $transaction->rollBack();
      throw $e;
  }
  
  echo 'success!';
  
  ```
  
  ## Craft 2
  
  ```php
  <?php
  namespace Craft;
  
  // Initialize Craft without letting it take over the whole request
  $app = require '../craft/app/bootstrap.php';
  
  // Set Craft to Craft Personal so we don't get any errors about Client/Pro-only components not being available
  $app->getInfo()->edition = 0;
  
  // Start a transaction in case anything goes south
  $transaction = $app->db->beginTransaction();
  try
  {
      // Fetch the entry you need to move
      $entry = $app->entries->getEntryById(184);
  
      // Remove it from its old structure
      $oldSection = $entry->getSection();
  
      $record = StructureElementRecord::model()->findByAttributes(array(
          'structureId' => $oldSection->structureId,
          'elementId'   => $entry->id
      ));
  
      if (!$record->deleteNode())
      {
          throw new Exception('Could not delete the old structure node');
      }
  
      // Set the new sectionId and typeId on the entry
      $entry->sectionId = 2;
      $entry->typeId = 2;
  
      // Append it to the end of the new structure
      $newSection = $entry->getSection();
  
      if (!$app->structures->appendToRoot($newSection->structureId, $entry, 'insert'))
      {
          throw new Exception('Could not insert the new structure node');
      }
  
      // Save the entry
      if (!$app->entries->saveEntry($entry))
      {
          throw new Exception('Could not save the entry');
      }
  
      // Commit the transaction
      $transaction->commit();
  
      echo 'success!';
  }
  catch (\Exception $e)
  {
      // Undo whatever changes we already made
      $transaction->rollback();
  
      throw $e;
  }
  
  ```
  
  Plug the right entry/section/type IDs in there, point your browser to <http://example.com/move_entry.php> and you should be all set!
  
  **Warning:** Make sure you never run this code for an entry that has any children in the structure, as its children will end up getting removed from the hierarchy.
  
  > **Comments:**
  > **username_0**: Thanks for the detailed answer username_1! But unfortunately I am getting the success message but the entry hasn't moved into the new structure.  
  > **username_1**: @username_0 I had tested locally and it worked. Are you sure you put all the right IDs in there? If so, maybe just send a DB backup over to support@buildwithcraft.com  
  > **username_0**: Sorry username_1, I've just run another test through and it seems to have worked fine and parenting is working as well, so thanks for that and sorry for taking up your time on this! One little thing however, if I run this on a parent entry, and then run it again on one of it's child entries (which have been moved to top level because the parent has gone), I get: "Fatal error: Call to a member function deleteNode() on a non-object in /vagrant/public/move_entry.php on line 25" Just a little thing as I will remember to move child entries first, but otherwise it's all good so thanks again.  
  > **username_1**: @username_0 Ah yes, didn’t think about that. Calling `deleteNode()` will remove that entry and all of its decedents from the hierarchy, so yeah, only do this with entries that don’t have any children.  
  > **username_2**: Is it possible to move an Entry and it's children?  
  > **username_1**: @username_2 your best bet would just be to move them one at a time, and re-organize them once they're all moved.  
  > **username_3**: Its worth noting, you can actually move a parent and its children in one go, but when they are moved, you'll lose the hierarchy structure, so you still have to replicate it, but you can pass an array on the ids and entry types.  
  > **username_4**: Is this still good for Craft 3.6.15 and in a multi-site ? Move entry to another section in another site ?  
  > **username_5**: How to do this in Craft CMS 4? When I follow these instructions, I get a 404 when trying to call the php-file (also after changing the htaccess).  
  > **username_5**: I managed to run the script through another way in Craft CMS 4, but it doesn't seem to work with Craft CMS 4. I'd appreciate if someone could make a Craft CMS 4-version of this script, since I have many complex entries to move in multiple languages (so too much manual work) and I don't know php.  
  
  ---
  
  