I’m trying to programmatically update a custom-type post with revisions enabled via form, and everything seems to be working fine except the post_author field for the revision is being set to 0 instead of the ID I’m passing in my array. All of the revisions get a 0, but the post itself gets the correct author ID. I’m not getting a WP_Error thrown either. It’s just ignoring the input. I’ve tried both wp_insert_post and wp_update_post with the same data, same results. The revisions and main post all save/create fine except for the post_author field in the revisions.
Relevant function code:
function yq_process_answer_submission_callback() {
// first check if data is being sent and that it is the data we want
if ( isset( $_POST["post_var"] ) ) {
$params = array();
parse_str($_POST["post_var"], $params);
$name = get_userdata( $params['yq_author_id'] )->display_name;
$posessive_name = $name.'''.($name[strlen($name) - 1] != 's' ? 's' : '');
$title = $posessive_name . ' Response to Question: ' . get_the_title( $params['yq_question_id'] );
$post = array(
'ID' => $params['yq_answer_id'],
'post_title' => $title,
'post_name' => sanitize_title_with_dashes($title, '', 'save'),
'post_content' => $params['yq_body'],
'post_type' => 'yq_answer',
'post_author' => $params['yq_author_id'],
'post_status' => 'private',
'post_parent' => $params['yq_question_id'],
'comment_status'=> 'open',
'ping_status' => 'closed',
);
if ($params['yq_answer_id'] == "") {
$the_post = wp_insert_post( $post, true );
} else {
$the_post = wp_update_post( $post, true );
}
if (is_wp_error($the_post)) {
echo "There was an error. Please try submitting again.rError message: ".print_r($the_post->get_error_message());
} else {
add_post_meta($the_post, 'to_instructor', $params['yq_send_to'], true);
add_post_meta($the_post, 'instructor_notified', false, true);
update_post_meta($the_post, 'to_instructor', $params['yq_send_to']);
update_post_meta($the_post, 'instructor_notified', false);
$response = array('question_id'=> get_post($the_post)->post_parent,
'answer_id' => $the_post,
'message'=>"Your response has been saved. It will be emailed to your instructor shortly."
);
echo json_encode($response);
}
die();
}
}
And also the init code for “answer” post type:
function create_yq_answers_post_type() {
register_post_type( 'yq_answer',
array(
'labels' => array(
'name' => 'Answers',
'singular_name' => 'Answer',
'menu_name' => 'Answers',
'add_new' => 'Add Answer',
'add_new_item' => 'Add New Answers',
'edit' => 'Edit',
'edit_item' => 'Edit Answers',
'new_item' => 'New Answers',
'view' => 'View Answers',
'view_item' > 'View Answers',
'search_items' > 'Search Answers',
'not_found' => 'No Answers Found',
'not_found_in_trash' => 'No Answers Found in Trash',
),
'description' => 'Answers type for questions.',
'capability_type' => 'answer',
'hierarchical' => false,
'supports' => array('title','author','editor','comments','revisions'),
'public' => false,
'has_archive' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 25,
'rewrite' => array('slug' => 'answers')
)
);
}