How can I set which page template this page, created using wp_insert_post
, uses? I want it to use template-blog.php
This is my current function;
add_action( 'admin_init', 'mytheme_admin_init' );
function mytheme_admin_init() {
if ( get_option( 'mytheme_installed' ) != true ) {
$new_page = array(
'slug' => 'blog',
'title' => 'Blog',
'content' => ""
);
$new_page_id = wp_insert_post( array(
'post_title' => $new_page['title'],
'post_type' => 'page',
'post_name' => $new_page['slug'],
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => $new_page['content'],
'post_status' => 'publish',
'post_author' => 1,
'menu_order' => 0
));
update_option( 'mytheme_installed', true );
}
}
From the
wp_insert_post()
documentation, thepage_template
argument reads as follow:So, you need to insert the page using
wp_insert_post()
and assign the page template using thepage_template
argument:Or, if you want to set a “page template” for another post type: