Insert new post in to wordpress from php

less than 1 minute read

This code snippet should let you add a new post to your wordpress database 2.5.1

require_once('wp-config.php');

// create post object class wm_mypost {     var $post_title;     var $post_content;     var $post_status;    / publish, private /     var $post_author;    / author user id (optional) /     var $post_name;      / slug (optional) /     var $post_type;      / ‘page’ or ‘post’ (optional, defaults to ‘post’) /     var $comment_status; / open or closed for commenting (optional) /     var $post_category ;  }

// initialize post object $wm_mypost = new wm_mypost(); $wm_mypost->post_title    = "Title2 ". date( ‘r’ ); $wm_mypost->post_content  = "content3"; $wm_mypost->post_status   = ‘publish’;  $wm_mypost->post_author   = 1;

// Catagorys $post_category = split("," , "one"); foreach($post_category as $key=>$val) {     $post_category[$key] = get_cat_ID($val); } $wm_mypost->post_category =  $post_category ; 

// Optional; uncomment as needed // $wm_mypost->post_type = ‘page’; // $wm_mypost->comment_status = ‘closed’;

// feed object to wp_insert_post $post_ID = wp_insert_post($wm_mypost); echo date( ‘r’ ) . "\n"; echo "post_ID:". $post_ID . "\n"; </code>

Leave a comment