Title: wordpress inserting posts programatically
Last modified: August 20, 2016

---

# wordpress inserting posts programatically

 *  [hiprakhar](https://wordpress.org/support/users/hiprakhar/)
 * (@hiprakhar)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-inserting-posts-programatically/)
 * I have to write a service that will automatically add posts in wordpress.
 * I know the following code can help, but I am not sure how to use it. If I write
   in inside the functions.php file, I will have no control over it.
 *     ```
       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);
       ```
   
 * Someone please tell me how to write a service using this code in php

Viewing 1 replies (of 1 total)

 *  Thread Starter [hiprakhar](https://wordpress.org/support/users/hiprakhar/)
 * (@hiprakhar)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-inserting-posts-programatically/#post-2141769)
 * I finally got the answer to the problem.
 * To make _this _code work:
 *     ```
       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);
       ```
   
 * we need to make sure that wordpress’ bootstrap has been started… WordPress bootstrap
   ensures that all wordpress configuration has been loaded into the memory. This
   includes all the core functions etc.
 * Coming back to the original problem of “inserting posts programatically”, we 
   need to call wp_insert_post() at the appropriate place _after_ starting the wp
   bootstrap.
 * For this create a new php file like [http://www.yourdomain.com/wpinstalldir/autoposts.php](http://www.yourdomain.com/wpinstalldir/autoposts.php)
 *     ```
       <?php
           /**
            * Writes new posts into wordpress programatically
            *
            * @package WordPress
            */
   
           /** Make sure that the WordPress bootstrap has run before continuing. */
           require(dirname(__FILE__) . '/wp-load.php');
   
           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);
           ?>
       ```
   
 * Now when you will execute this script at [http://www.yourdomain.com/wpinstalldir/autoposts.php](http://www.yourdomain.com/wpinstalldir/autoposts.php)
   your post will be created. Easy and simple!
 * Just adding the line `require(dirname(__FILE__) . '/wp-load.php');` made all 
   the difference.

Viewing 1 replies (of 1 total)

The topic ‘wordpress inserting posts programatically’ is closed to new replies.

## Tags

 * [automatic posts](https://wordpress.org/support/topic-tag/automatic-posts/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 1 reply
 * 1 participant
 * Last reply from: [hiprakhar](https://wordpress.org/support/users/hiprakhar/)
 * Last activity: [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-inserting-posts-programatically/#post-2141769)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
