Automatically create child pages and grand child pages when saving a (parent) page

Question

I have a bit of a tricky one…

I have a hierarchical Custom Post Type (‘shows’) that represents events. Is it possible for the user to create a new page (ie show), save the page and for WordPress to automatically create a defined set of child pages with defined names and grandchild pages (child of one of them)?

Here is something similar:

function wpa8582_add_show_children( $post_id ) {
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )
return;

if ( !wp_is_post_revision( $post_id )
&& 'show' == get_post_type( $post_id )
&& 'auto-draft' != get_post_status( $post_id ) ) {  
    $show = get_post( $post_id );
    if( 0 == $show->post_parent ){
        $children =& get_children(
            array(
                'post_parent' => $post_id,
                'post_type' => 'show'
            )
        );
        if( empty( $children ) ){
            $child = array(
                'post_type' => 'show',
                'post_title' => 'About',
                'post_content' => '',
                'post_status' => 'draft',
                'post_parent' => $post_id,
                'post_author' => 1,
                'tax_input' => array( 'your_tax_name' => array( 'term' ) )
            );
            wp_insert_post( $child );
        }
    }
}

} add_action( ‘save_post’, ‘wpa8582_add_show_children’ );

And here is what I need:

//Save parent page
London

//Children automatically created

/LONDON

  1. About
  2. Visitor Info
    • One grandchild page of VISITOR INFO
    • Two grandchild page of VISITOR INFO
    • Three grandchild page of VISITOR INFO
  3. Exhibitors
  4. Sponsors
  5. Press
0
, Fran Marin 8 months 2020-07-02T15:12:09-05:00 0 Answers 74 views 0

Leave an answer