Define page template in wp_insert_post

From the wp_insert_post() documentation, the page_template argument reads as follow:

page_template: If post_type is ‘page’, will attempt to set the page
template. On failure, the function will return either a WP_Error or 0,
and stop before the final actions are called. If the post_type is not
‘page’, the parameter is ignored. You can set the page template for a
non-page by calling update_post_meta() with a key of
‘_wp_page_template’.

So, you need to insert the page using wp_insert_post() and assign the page template using the page_template argument:

add_action( 'admin_init', 'mytheme_admin_init' );
function mytheme_admin_init() {
    if ( ! get_option( 'mytheme_installed' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'Blog',
            'post_type'      => 'page',
            'post_name'      => 'blog',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,
            // Assign page template
            'page_template'  => 'template-blog.php'
        ) );

        update_option( 'mytheme_installed', true );
    }
}

Or, if you want to set a “page template” for another post type:

add_action( 'admin_init', 'mytheme_admin_init' );
function mytheme_admin_init() {
    if ( ! get_option( 'mytheme_installed' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'Blog',
            'post_type'      => 'my_custom_post_type',
            'post_name'      => 'blog',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0
        ) );

        if ( $new_page_id && ! is_wp_error( $new_page_id ) ){
            update_post_meta( $new_page_id, '_wp_page_template', 'template-blog.php' );
        }

        update_option( 'mytheme_installed', true );
    }
}

Leave a Comment