I know that I can programmatically create a new page via wp_insert_post setting 'post-type' to 'page.'

How do I go about editing that page programmatically? (title, content, slug, etc.)

Also, if I wanted to set the template for that page programmatically, how would I do that?


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.

  • What about my first question? – Eli Mar 12 '19 at 17:00
  • 1
    @Eli if you look up the docs there's a wp_update_post, which is just a wrapper around wp_insert_post, but keep in mind we have a 1 question per question policy here, don't "mung" or "squish" all your questions together, it can drastically reduce the opportunities for answers – Tom J Nowell Mar 12 '19 at 17:05
  • To edit/update the data, you can pass ID to the $post_arr, if a post with that ID exists, wp_insert_post will update old data instead of creating a new one. – hamdirizal Mar 12 '19 at 17:06

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.