I have following function to create new woocommerce product on the frontend.
parse_str($_POST['form_data'], $form_data);
$insertPost = array(
//'ID' => $post_id,
'post_title' => $form_data['post_title'],
'post_status' => $form_data['post_status'],
'post_type' => 'product',
'post_content' => $form_data['post_content']
);
$insert_post = wp_insert_post( $insertPost, true);
update_post_meta( $insert_post, 'hours', $form_data['hours'] );
update_post_meta( $insert_post, 'year', $form_data['year'] );
update_post_meta( $insert_post, '_weight', $form_data['_weight'] );
update_post_meta( $insert_post, '_price', $form_data['_regular_price'] );
update_post_meta( $insert_post, '_regular_price', $form_data['_regular_price'] );
update_post_meta( $insert_post, '_sale_price', $form_data['_sale_price'] );
update_post_meta( $insert_post, '_stock_status', $form_data['_stock_status'] );
$term_taxonomy_ids = wp_set_object_terms( $insert_post, array(15), 'product_cat' );
wp_set_object_terms( $insert_post, 'simple', 'product_type' );
The code above is creating a new product and adding all data to it as expected. The issue is when using the sort by price filter, the sorting doesn't really sort the newly product created in proper way, it just ignores it.
After searching i found in WooCommerce > Status > Tools > Product lookup tables (regenerate) once i click the regenerate button, then it regenerate all product data and the price sorting is working as expected.
So what is missing in my function to make the price sorting work without regenerate button?