Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I know how to create pages automatically when a theme is activated, but I need help to figure out how to also programmatically create child pages at the same time.

For example:

- Page 1
  - Page 1.1
  - Page 1.2
- Page 2
  - Page 2.1
  - Page 2.2
  - Page 2.3
- Page 3
  - Page 3.1
- Page 4
  - Page 4.1
  - Page 4.2

And here is my function for automatic page creation (top level only).

if (isset($_GET['activated']) && is_admin()){
    add_action('init', 'create_initial_pages');
}

function create_initial_pages() {
    $pages = array(
        'page1' => 'Page 1',
        'page2' => 'Page 2',
        'page3' => 'Page 3',
        'page4' => 'Page 4'
    );
    foreach($pages as $key => $value) {
        $id = get_page_by_title($value);
        $page = array(
            'post_type'   => 'page',
            'post_title'  => $value,
            'post_name'   => $key,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_parent' => ''
        );
        if (!isset($id)) wp_insert_post($page);
    };
}

Does anyone know how to extend this function to also create child pages (if they don't already exist) when the theme is activated?

share|improve this question
1  
Correct me if I'm wrong, but doesn't the post_parent argument that you're passing to wp_insert_post ring a bell? codex.wordpress.org/Function_Reference/wp_insert_post Just keep track of what page1...4 ID's are (wp_insert_post will return the IDs) and then iterate over the children assigning the relevant post parent IDs. Hope this helps. –  soulseekah Oct 16 '11 at 15:52
    
Thanks for pointing me in the right direction @Soulseekah! –  Paul Oct 16 '11 at 22:38
add comment

2 Answers

As @Soulseekah said, you can do this with post_parent. I didn't test with the following code, but it should work

$pages = array( 
    array(
        'name' => 'page1',
        'title' => 'Page 1',
        'child' => array(
            array(
                'name' => 'page11',
                'title' => 'Page 1.1'
            ),
            array(
                'name' => 'page12',
                'title' => 'Page 1.2'
            )
        )
    ),
    array(
        'name' => 'page2',
        'title' => 'Page 2',
        'child' => array(
            array(
                'name' => 'page21',
                'title' => 'Page 2.1'
            ),
            array(
                'name' => 'page22',
                'title' => 'Page 2.2'
            )
        )
    ),
    array(
        'name' => 'page3',
        'title' => 'Page 3',
        'child' => array(
            array(
                'name' => 'page21',
                'title' => 'Page 2.1'
            ),
            array(
                'name' => 'page22',
                'title' => 'Page 2.2'
            )
        )
    ),
);

$template = array(
    'post_type' => 'page',
    'post_status' => 'publish',
    'post_author' => 1
);

foreach( $pages as $page ) {
    $exists = get_page_by_title( $page['name'] );

    if( !$exists ) {
        $my_page = array(
            'post_name' => $page['name'],
            'post_title' => $page['title']
        );
        $my_page = array_merge( $my_page, $template );

        $id = wp_insert_post( $my_page );

        //if there is any child page, create them by {$id} as 'post_parent'
        if( isset( $page['child'] ) ) {
            foreach( $page['child'] as $child ) {
                $child_page = array(
                    'post_name' => $child['name'],
                    'post_title' => $child['title'],
                    'post_parent' => $id
                );
                $child_page = array_merge( $child_page, $template );
                $id = wp_insert_post( $child_page );
            }
        }
    }
}
share|improve this answer
    
Exactly what I was looking for! Thanks @Tareq for the example. –  Paul Oct 16 '11 at 22:37
add comment
up vote 1 down vote accepted

The example by @Tareq was very helpful, but instead of creating multiple child pages for the parent page, it would make each child page a sub-parent page.

Page 1
- Page 1.1
  -- Page 1.2
     --- Page 1.3
Page 2
- Page 2.1
  -- Page 2.2

etc.

Here is the fixed/improved function (I'm sure that this can be improved even more as the page checks are not that thorough):

function create_initial_pages() {
    $pages = array(
        array(
            'name'  => 'page1',
            'title' => 'Page 1',
            'child' => array(
                'page1-1' => 'Page 1.1',
                'page1-2' => 'Page 1.2',
                'page1-3' => 'Page 1.3',
                'page1-4' => 'Page 1.4'
            )
        ),
        array(
            'name'  => 'page2',
            'title' => 'Page 2',
            'child' => array(
                'page2-1' => 'Page 2.1',
                'page2-2' => 'Page 2.2',
                'page2-3' => 'Page 2.3'
            )
        )
    );

    $template = array(
        'post_type'   => 'page',
        'post_status' => 'publish',
        'post_author' => 1
    );

    foreach( $pages as $page ) {
        $exists = get_page_by_title( $page['title'] );
        $my_page = array(
            'post_name'  => $page['name'],
            'post_title' => $page['title']
        );
        $my_page = array_merge( $my_page, $template );

        $id = ( $exists ? $exists->ID : wp_insert_post( $my_page ) );

        if( isset( $page['child'] ) ) {
            foreach( $page['child'] as $key => $value ) {
                $child_id = get_page_by_title( $value );
                $child_page = array(
                    'post_name'   => $key,
                    'post_title'  => $value,
                    'post_parent' => $id
                );
                $child_page = array_merge( $child_page, $template );
                if( !isset( $child_id ) ) wp_insert_post( $child_page );
            }
        }
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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