Move entry from one structure to another with parenting Seeing as moving entries between sections isn't supported (yet) through the Craft interface, I followed Brandon's answer 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?
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

&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!';


Craft 2

&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;
}


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.