plugin development - How to edit the properties of a page programmatically?

admin2025-06-02  3

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?

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?

Share Improve this question edited Mar 12, 2019 at 16:46 Eli asked Mar 12, 2019 at 16:34 EliEli 691 gold badge2 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748814591a313946.html

最新回复(0)