Post Pic

WordPress tip: Insert posts programmatically

Do you know that it is possible to insert post in WordPress programmatically? In this recipe, I’ll show you how to do it easily.

Just paste the following code anywhere on WordPress theme files. If you want to test, I recommend pasting it in your functions.php file.
That's all you have to do. Once executed, this code will insert a new post into WordPress database.

global $user_ID;
$new_post = array(
    'post_title' => 'My New Post',
    'post_content' => 'Lorem ipsum dolor sit amet...',
    'post_status' => 'publish',
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => $user_ID,
    'post_type' => 'post',
    'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);

Thanks to Matt Harzewski for this great piece of code!

One Response

Apr 30 2012 11:49

it is not working even gave following errors

Fatal error: Call to undefined function wp_insert_post() in /home/fazam/public_html/wscraper/wp-includes/functions.php on line 35

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox