$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - How to edit the properties of a page programmatically?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far