• 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)
  • 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

    (@bcworkz)

    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.

    Thread Starter xvilo

    (@xvilo)

    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.