There are many ways you can achieve this and it’s pretty hard to say which one will be the best.
First part would be to register your CPTs – this will be easy, I guess. So let’s say you already have your CPTs registered and these CPTs are called fruit
and vegetable
.
So the only thing to do now is to convert existing pages to CPTs.
What will we need?
- IDs of parent pages,
- ability to run SQL code.
How to do this?
Just run this query:
UPDATE prefix_posts
SET post_parent = 0, post_type="vegetable"
WHERE post_type="page" AND post_parent = <VEGETABLE_PAGE_ID>
And the PHP version, because it may be a little bit easier to run it this way:
function convert_children_of_page_to_cpt( $page_slug, $cpt_slug ) {
global $wpdb;
$page = get_page_by_path( $page_slug );
if ( $page ) {
$wpdb->update(
$wpdb->posts,
array( 'post_parent' => 0, 'post_type' => $cpt_slug ),
array( 'post_parent' => $page->ID, 'post_type' => 'page' )
);
}
}
DISCLAIMER: Of course you should backup your database before doing any of these queries 😉