Title: Programmatically add posts from api.
Last modified: September 22, 2016

---

# Programmatically add posts from api.

 *  [xvilo](https://wordpress.org/support/users/xvilo/)
 * (@xvilo)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/programmatically-add-posts-from-api/)
 * Hi,
 * We have a 3rd party API. The entries of the API needs to be imported into a custom
   posttype.
    The unfortunate thing is that I’m having some trouble with this.
 * Currently I am using this code:
 *     ```
       $post = array(
       	'post_author' => 2,
       	'post_content' => "{$xml->opmerkingen}",
       	'post_name' => "{$xml->titel}",
       	'post_status' => 'publish',
       	'post_excerpt' => "{$xml->opmerkingen}",
       	'post_title' => "{$xml->titel}",
       	'post_type' => 'voertuig'
       );			
       wp_insert_post($post);
       ```
   
 * And it is giving me these erros.
 * `Notice: Trying to get property of non-object in wp-includes/post.php on line
   3578`
 * `Notice: Trying to get property of non-object in wp-includes/link-template.php
   on line 49`
 * `Fatal error: Call to undefined function is_user_logged_in() in wp-includes/post.
   php on line 2066`
 * I hope somebody could help me out with this!
 * Thanks in advance,
 * Sem

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [ancawonka](https://wordpress.org/support/users/ancawonka/)
 * (@ancawonka)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/programmatically-add-posts-from-api/#post-8211794)
 * Where/when are you running this code? In a theme? A plugin? What hook?
 * Are you properly escaping quotes and other special characters in the values coming
   from your API?
 * Have you looked at what’s on line 3578 of wp-includes/post.php? That should tell
   you what value is breaking where.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/programmatically-add-posts-from-api/#post-8212866)
 * Based on those errors, it’s evident that you are not initializing the WP environment
   correctly. I think ancawonka is thinking along the same line, hence her first
   line of questions. Without knowing your answer to her questions, I can tell you
   there are basically only three proper ways to initialize the environment to run
   custom code. 1) AJAX requests sent through admin-ajax.php. 2) Create a custom
   page template to contain your code, create a page based on it, and request that
   page. 3) Send requests through admin-post.php (GET requests work here too).
 * You’ll see many examples that require or include core files. This is doing it
   wrong. Explanation why and details on using admin-post.php can be found in [this article](http://glennmessersmith.com/pages/wpenviro.html).
 *  Thread Starter [xvilo](https://wordpress.org/support/users/xvilo/)
 * (@xvilo)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/programmatically-add-posts-from-api/#post-8229354)
 * Thank you, it was in fact, I was trying to insert the posts before wordpress 
   was fully loaded! Everything works as expected right now

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Programmatically add posts from api.’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 3 participants
 * Last reply from: [xvilo](https://wordpress.org/support/users/xvilo/)
 * Last activity: [9 years, 6 months ago](https://wordpress.org/support/topic/programmatically-add-posts-from-api/#post-8229354)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
