DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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 Comment

  1. 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

Leave a Comment

Your email address will not be published. Required fields are marked *