menus - Swap out a page that is a parent to a many many pages

I have a specific page on my site and I'm experimenting with some changes that are too complex to have a backup (pa

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/skin-care-treatment/skin/juvederm-voluma-2/ This is what it was originally

site/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?

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/skin-care-treatment/skin/juvederm-voluma-2/ This is what it was originally

site/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?

Share Improve this question asked Mar 18, 2020 at 20:32 fudgefudge 1173 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744657001a4586242.html

相关推荐

  • menus - Swap out a page that is a parent to a many many pages

    I have a specific page on my site and I'm experimenting with some changes that are too complex to have a backup (pa

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信