Please try:
$args = array(
'posts_per_page' => -1,
'post_type' => 'thread'
);
$thread_posts = get_posts( $args ); // Get all posts of thread.
foreach($thread_posts as $thread_post_object) {
/*not sure really need to be working IDs only, but is simpler, plus made checking was getting right ones easier */
$thread_post_IDs[] = $thread_post_object->ID;
}
foreach ($thread_post_IDs as $thread_post_ID) {
$comments_query = new WP_Comment_Query;
$thread_comments = $comments_query->query(array
(
'post_id' => $thread_post_ID,
'status' => 'approve'
)
);
foreach ($thread_comments as $thread_comment) {
$reply_post = array(
'post_status' => 'publish', // Set replies status.
'post_type' => 'answer', // Set post type, post type must be answer.
'post_parent' => $thread_post_ID, // Set id of question to make relation between question and answer
'post_author' => $thread_comment->user_id, // Set comment user id as post id - requires registered users otherwise returns 0
'post_title' => 'RE: ' . get_the_title($thread_post_ID), //since have ID only need alternative way to get title
'post_content' => $thread_comment->comment_content // Set comment content as post content.
);
//print_r($reply_post); // if you want to test it first
wp_insert_post($reply_post); // if you want to see what happens!
}
}