Building off @hrohh’s logic… this will also work when bulk editing…
public function override_type( $data = array(), $postarr = array() ) {
// Bail if form field is missing
if ( empty( $_REQUEST['pts_post_type'] ) || empty( $_REQUEST['pts-nonce-select'] ) ) {
return $data;
}
// Get post IDs
$post_ids = array();
if ( isset( $_REQUEST['bulk_edit'] ) && ! empty( $_REQUEST['post'] ) ) {
$post_ids = array_map( 'intval', (array) $_REQUEST['post'] );
} elseif ( ! empty( $_REQUEST['post_ID'] ) ) {
$post_ids = array( intval( $_REQUEST['post_ID'] ) );
}
// Bail if post ID is invalid
if ( ! ( $post_ids && in_array( $postarr['ID'], $post_ids ) ) ) {
return $data;
}
// Post type information
$post_type = sanitize_key( $_REQUEST['pts_post_type'] );
$post_type_object = get_post_type_object( $post_type );
// Bail if empty post type
if ( empty( $post_type ) || empty( $post_type_object ) ) {
return $data;
}
// Bail if user cannot 'edit_post'
if ( ! current_user_can( 'edit_post', $postarr['ID'] ) ) {
return $data;
}
// Bail if nonce is invalid
if ( ! wp_verify_nonce( $_REQUEST['pts-nonce-select'], 'post-type-selector' ) ) {
return $data;
}
// Bail if autosave
if ( wp_is_post_autosave( $postarr['ID'] ) ) {
return $data;
}
// Bail if revision
if ( wp_is_post_revision( $postarr['ID'] ) ) {
return $data;
}
// Bail if it's a revision
if ( in_array( $postarr['post_type'], array( $post_type, 'revision' ), true ) ) {
return $data;
}
// Bail if user cannot 'publish_posts' on the new type
if ( ! current_user_can( $post_type_object->cap->publish_posts ) ) {
return $data;
}
// If post type has changed...
if ( $data['post_type'] !== $post_type ) {
// Set post parent to 0
$data['post_parent'] = 0;
// Update post type
$data['post_type'] = $post_type;
}
// Return modified post data
return $data;
}