Action hook “wp_insert_post” works but not for last imported post

The ‘wp_insert_post’ action is fired for each post that is inserted and passed the ID of the inserted post to the callback function, so I think your $wpdb query is unnecessary – in fact, I think you’re running the “custom function” portion for all published posts, not just the inserted post (which may or may not lead to issues depending on what you’re actually doing).

function on_post_import( $post_id ) {
  // custom function using $post_id
}
add_action( 'wp_insert_post', 'on_post_import' );