5

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 );
}
}
10

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 );
    }
}
  • Can you link to that quote? – Michael Apr 12 '16 at 2:37
  • 2
    This answer is very old, I couldn't find the reference for that quote, but I've found that the answer was not totally correct. It was working fine but in case of page post type, the page template can be set directly using wp_insert_post(). See updated answer. – cybmeta Apr 12 '16 at 6:14
  • 2
    Since I had to debug the core to find out, I think it's also worth noting that the template parameter is relative to your theme folder. So in case you keep templates under a /templates subdirectory, you should pass templates/template-blog.php as template parameter. – GigiSan Dec 7 '18 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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