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
版权声明:本文标题:api - Is it possible to use 'wp_insert_post' function within a for loop? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742305311a2449797.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论