K. Kilian Lindberg
K. Kilian Lindberg

Reputation: 2976

Setting a new page with wp_insert_post as start page

I'm creating an APP with a Wordpress child theme. It has a start page created with wp_insert_post when activating the theme. How do I set this page as startpage with PHP in functions.php?

// Install theme
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {
        $last_id = wp_insert_post(array(
            'post_type' => 'page',
            'post_title' => 'Welcome to this wonderful page!',
            'post_content' => 'Holy smoke',
            'post_name' => 'startpage',
            'post_status' => 'publish',
            'comment_status' => 'closed'
        ));
        update_post_meta($last_id, "_wp_page_template", "page.php");

// Set this page as startpage... but how? 

} // Install theme

Upvotes: 1

Views: 587

Answers (1)

brasofilo
brasofilo

Reputation: 26065

If understood correctly, you are searching for the option page_on_front:

page on front

Just use:

// Set "static page" as the option
update_option( 'show_on_front', 'page' );

// Set the front page ID
update_option( 'page_on_front', $last_id );

Upvotes: 2

Related Questions