Manually Insert Page/Post Into WordPress Via PHP

When I wrote a page to automatically process photos into individual posts using WordPress to make a gallery, I ran into the problem of manually creating a post and having it set the category. I solved the problem and at least one person has mentioned that they’d like to see how it’s done so here it is:
class poster {
var $post_title;
var $post_content;
var $post_category;
var $post_status;
var $post_author;
var $post_name;
}
$a_post = new poster();
$a_post->post_title = “Title of the post.”;
$a_post->post_content = “Here’s where your content goes”;
// An array of category ids. Note that this failed for me when the category wasn’t created ahead of time
$a_post->post_category = array(1,7);
// Or private or draft or whatever
$a_post->post_status = “publish”;
// I’m author 1. You probably are too, but set this to whatever you want.
$a_post->post_author = 1;
//This should probably be based on some variable or function, but I’ll hard-code it for this example
$a_post->post_name = “smithy”;
// feed object to wp_insert_post
$post_id = wp_insert_post($a_post);
Share This
|
|
|
|
|
|
|
||










