How to edit the properties of a page programmatically?

If you look at the documentation, you can insert title, content, slug, etc. using wp_insert_post then use update_post_meta to assign the template to that post.

You can write like this

$post_arr = array(
    'post_title'=>'My title goes here',
    'post_content'=>'This is my long content.',
    'post_excerpt' => 'My Excerpt',
    'post_status' => 'publish',
);
$new_post_id = wp_insert_post($post_arr);

Then set template like this.

update_post_meta( $new_post_id, '_wp_page_template',  'template-file.php' );

To see more options for wp_insert_post, you can check it’s documentation page.

Leave a Comment