Using wp_insert_post from a plugin
Submitted by benjy on Sunday, May 6, 2012 - 13:49.
You may want to add posts from a plugin. Probably because you are loading data in from elsewhere like I was here using the REA XML Parser.
Example
require_once("path/to/wp-config.php");
//setup post
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_id,
'post_type' => 'property',
'post_category' => array($category)
);
$post_id = wp_insert_post($new_post); //insert post
if($post_id != 0) {
//add metadata
add_post_meta($post_id, "_field1", esc_attr($field1));
add_post_meta($post_id, "_field2", esc_attr($field2));
}
Add new comment