admin管理员组

文章数量:1332377

I want to insert as a custom post type some data fethced from an API, I store that data and then pass it trough a for loop, then I use the loop data to create an array to pass it to wp_insert_post function, but it does not work, not even a single post is created, the code I'm trying to run is like this:

$url = "https://api_endpoint";
$response = file_get_contents($url);
$response = json_decode($response, true);
for ($i=0; $i <sizeof($response); $i++) { 
    $my_post = array(
        'post_title'    => $response[$i]['results'],
        'post_content'  => $response[$i]['content'],
        'post_status'   => 'draft',
        'post_author'   => 1,
        'post_type'     => 'custom_pt'
    );
    $post_id = wp_insert_post( $my_post, true );
}

I doublechecked the response of the API and I'm sure the correct data is sent to the function, also I tried the function inserting just one post and works fine, so I would like to know if there is some limitation to use the function (I have not found anything at the codex) or if I'm doing something wrong.

Thanks :).

Edit: I've actually tried using a request to wp-json/wp/v2/posts, also I catch the response with

if( !is_wp_error( $post_id ) ) 

and return the response stored un $post_id (it returns nothing, the site just does nothing and I have to restart apache to re enable it).

本文标签: apiIs it possible to use 39wpinsertpost39 function within a for loop