Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically insert WordPress child pages (and bbPress Forum) when publishing a page / post / custom post type (for functions.php)
<?php
//AUTO ADD WORDPRESS POST CHILDREN
function add_page_children( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !wp_is_post_revision( $post_id )
&& 'page' == get_post_type( $post_id ) // Change as per required post type
&& 'auto-draft' != get_post_status( $post_id ) ) {
$thispage = get_post( $post_id );
$ptitle = $thispage->post_title; // Set the Parent Page title as a variable to add to new pages
if( 0 == $thispage->post_parent ){
$children =& get_children(
array(
'post_parent' => $post_id,
'post_type' => 'page' // Change as per required post type
)
);
if( empty( $children ) ){
// 'Page Name News'
$pagenews = array(
'post_type' => 'page',
'post_title' => $ptitle . ' News',
'post_content' => '',
'post_status' => 'publish', // Change as per post status requirements
'post_parent' => $post_id,
'post_author' => 1
);
wp_insert_post( $pagenews );
// 'Page Name Documents'
$pagedocs = array(
'post_type' => 'page',
'post_title' => $ptitle . ' Documents',
'post_content' => '',
'post_status' => 'publish', // Change as per post status requirements
'post_parent' => $post_id,
'post_author' => 1
);
wp_insert_post( $pagedocs );
// 'Page Name Forum' (bbPress example)
$pageforum = array(
'post_type' => 'forum',
'post_title' => $ptitle . ' Forum',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1
);
wp_insert_post( $pageforum );
}
}
}
}
add_action( 'save_post', 'add_page_children' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.