The hook you choose is important here. I found success choosing the latest hook I could wp_insert_post
. If other functions are hooked to the same action they need to be considered because if they create/update posts too these hooks will be called again.
The codex lists the Post, Page, Attachment, and Category Actions (Admin) in sequence.
Working code for those interested.
function kt_bulk_add_inventory( $ID ) {
Global $typenow;
if ( 'inventory' != $typenow ) {
return;
}
// Bail if no Quantity provided
if ( ! isset( $_REQUEST['acf']['field_55bb25125095a'] ) || empty( $_REQUEST['acf']['field_55bb25125095a'] ) ) {
return;
}
$qty = $_REQUEST['acf']['field_55bb25125095a'];
$item_data = array(
'item_wood_type' => $_REQUEST['acf']['field_55795ed866432'],
'item_thickness' => $_REQUEST['acf']['field_555be6e572883'],
'item_width' => $_REQUEST['acf']['field_555be70972884'],
'item_length' => $_REQUEST['acf']['field_555be73a72885'],
'item_purchase_order' => $_REQUEST['acf']['field_55795fb753941'],
);
$wood_type_term = get_term_by( 'id', (int) $item_data['item_wood_type'], 'wood_type' );
$tax = array(
'wood_type' => $wood_type_term->name
);
$post_data = array(
'post_status' => 'publish',
'post_type' => 'inventory',
'tax_input' => $tax
);
$new_items = 0;
remove_action( 'wp_insert_post', 'kt_bulk_add_inventory', 12, 1 );
while ( 1 < $qty ) {
$item_id = wp_insert_post( $post_data );
foreach ( $item_data as $key => $value ) {
update_post_meta( $item_id, $key, $value );
}
$qty--;
$new_items++;
}
add_action( 'wp_insert_post', 'kt_bulk_add_inventory', 12, 1 );
}
add_action( 'wp_insert_post', 'kt_bulk_add_inventory', 12, 1 );