By Eli

2019-03-12 16:34:16 8 Comments

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?


@hamdirizal 2019-03-12 16:59:29

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.

@Eli 2019-03-12 17:00:40

What about my first question?

@Tom J Nowell 2019-03-12 17:05:35

@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

@hamdirizal 2019-03-12 17:06:09

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.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Set page template programaticlly in plugin

1 Answered Questions

[SOLVED] Adminbar Missing "Edit Page"

1 Answered Questions

[SOLVED] How to set post slug when using wp_insert_post();?

4 Answered Questions

[SOLVED] Hide page title

  • 2014-11-01 01:06:18
  • oOPurple_HazeOo
  • 2926 View
  • 0 Score
  • 4 Answer
  • Tags:   php pages title

1 Answered Questions

[SOLVED] How do you create a custom edit.php / edit pages page

1 Answered Questions

How to refresh a template page on Wordpress in 3 seconds - easiest method?

2 Answered Questions

Only echo Page Content (not page title & page content together)

  • 2015-03-08 07:43:13
  • user1822824
  • 813 View
  • -1 Score
  • 2 Answer
  • Tags:   functions pages

2 Answered Questions

2 Answered Questions

[SOLVED] How to get the excerpt of a page before more tag?

1 Answered Questions

[SOLVED] From a developer's point of view what are the differences between pages and posts?

  • 2014-03-03 21:51:47
  • 4th_dimention
  • 45 View
  • 0 Score
  • 1 Answer
  • Tags:   pages

Sponsored Content