[wordpress][solution] How to code (API) a new post or page insert ..

By.

min read

Share

How to write your own code to insert a post or a page? Eazy! just use wp_insert_post() 🙂

Here you go:
http://codex.wordpress.org/Function_Reference/wp_insert_post
Warning: do not use $my_post[‘post_author’] = 1; as stated on the codex page, use 2 for the main admin.

[code:1:da679056c2]// Create post object
$my_post = array();
$my_post[’post_title’] = ‘My post’;
$my_post[’post_content’] = ‘This is my post.’;
$my_post[’post_status’] = ‘publish’;
$my_post[’post_author’] = 2;
$my_post[’post_category’] = array(8,39);

// Insert the post into the database
wp_insert_post( $my_post );
[/code:1:da679056c2]
[code:1:da679056c2]$defaults = array(
‘post_status’ => ‘draft’,
‘post_type’ => ‘post’,
‘post_author’ => $user_ID,
‘ping_status’ => get_option(’default_ping_status’),
‘post_parent’ => 0,
‘menu_order’ => 0,
‘to_ping’ => ”,
‘pinged’ => ”,
‘post_password’ => ”,
‘guid’ => ”,
‘post_content_filtered’ => ”,
‘post_excerpt’ => ”
);
[/code:1:da679056c2]

Leave a Reply

Your email address will not be published. Required fields are marked *