article

Monday, February 28, 2011

get the ID of the last insert post in WordPress

get the ID of the last insert post in WordPress

<div id="div1">
<?php
//insert new post
// Create post object
$my_post = array();
$my_post['post_title'] = 'Hello world';
$my_post['post_content'] = 'This is a sample post';
$my_post['post_status'] = 'published';
$my_post['post_author'] = 7; //the id of the author
$my_post['post_category'] = array(10,12); //the id's of the categories

// Insert the post into the database
$post_id = wp_insert_post( $my_post ); //store new post id into $post_id

//now add tags to new post
$tags = array('html', 'css', 'javascript');
wp_set_object_terms( $post_id, $tags, 'post_tag', true );

?>
</div>

Related Post