0

I have a specific page on my site and I'm experimenting with some changes that are too complex to have a backup (page template change, custom fields etc).

I duplicated the page using a plugin and made all the changes to this new page, including adding all the links to the child pages. I renamed the URL slug of the old page (now the backup) and then renamed the duplicate page to the old page's original URL. When I went to click the links I kept getting 404's until I realized that the child pages have a parent slug pointing to the old page. Here is what I'm talking about:

site.com/skin-care-treatment/skin/juvederm-voluma-2/ This is what it was originally

site.com/skin-care-treatment-old/skin/juvederm-voluma-2/ All the pages were automatically changed to this

How do I fix this? Can I swap out the old page for the new somehow?

1 Answer 1

0

Make a database backup first!

You can create such code in theme's functions.php and execute it once.

like https://your.site/?old_parent=100&new_parent=200

Where 100 is old parent page Id and 200 new parent page id you'd like to set for child pages of old parent page.

Set your own IDs! (id is a numerical value in URL when you edit post\page for example http://yoursite.test/wp-admin/post.php?post=54&action=edit - page id is 54)

function custom_replace_parent() {

    $old_parent_post_id = intval( $_GET['old_parent'] );
    $new_parent_post_id = intval( $_GET['new_parent'] );

    if( $old_parent_post_id  < 1 ) {return;}

    $child_pages = get_posts(
        array(
            'post_type'      => 'page',
            'post_status'    => 'any',
            'posts_per_page' => -1,
            'post_parent'    => $old_parent_post_id,
        )
    );

    foreach ( $child_pages as $child_page ) {

        wp_update_post(
            array(
                'ID'          => $child_page->ID,
                'post_parent' => $new_parent_post_id,
            )
        );

    }

}

add_action( 'init', 'custom_replace_parent' );

If you'll hit PHP max execution time, just refresh a page (it may happen if you have thousands of child pages for this single parent page).

Important! Remove this code out of your site's functions.php after you'll finish

And again, make a backup first!

The idea of this code is to be put, then you do replacements refreshing your site with GET parameters as in example but with your page ids in place, than this code MUST be removed from site.

If you will not remove this code after you've done fixing it may make harm. as really anyone who might guess that this code is installed on your site will be able to perform these actions, ruining your website!

3
  • Thank you. Since this is running at 'init', it would just have to run any page at all to fire it off?
    – fudge
    Mar 19, 2020 at 13:25
  • Oh wait, you're saying I'd fire it off by your.site/?old_parent=100&new_parent=200 replacing it with the appropriate post values.
    – fudge
    Mar 19, 2020 at 13:27
  • Please see I've added line so it will not fire if old_parent isn't present. The idea of this code is to be put, then you do replacements refreshing your site with GET parameters as in example but with your page ids in place, than code MUST be removed. As it may make harm.
    – Mikhail
    Mar 19, 2020 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.