Using wp_insert_post from a plugin

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

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.