I've run into a weird situation, and I have no clue about what's going wrong.
For a customer, I'm importing information from a proprietary system into WordPress. The customers' system is multilingual (Dutch and English), and I'm using categories to preserve that distinction (I could use post_meta for that, I guess, but categories are slightly easier to filter output on). Since the proprietary database runs on the same server as the WordPress install, I fetch the info from the database, and loop through the results. On each iteration, I split the information in two parts (one for each language), and do two wp_insert_post()'s, after which I set the correct categories (using wp_set_post_cats() and the post ID returned by wp_insert_post()) and post_meta.
Somehow, however, the post_ID's get mixed up. From what I've been able to gather, by echo()'ing stuff every few lines, the returned post_ID isn't always a value you'd expect. Sometimes, two iterations will be on the same post_ID (which means that I've lost one language), sometimes it will skip a few id's (so it might do 5, 6, 9, 7, 8), and most of the time it actually does what it's supposed to do. So in the end, I have a (WP) database that's mostly correct, some ID's aren't consecutive (which isn't a problem), and some information will be missing (which is one hell of a problem).
What baffles me is that I can't figure out where things go wrong. I explicitly set the variable that the post_ID will be returned into to 0 on each iteration, so the doubles are returned by wp_insert_post(). I *am* pretty sure that all posts are INSERTed (not UPDATEd), so the ID returned SHOULD be the mysql_insert_id.
I've also tried inserting a usleep() after each insert, but even if I let the script wait for half a second, the doubles are there. I think I can rule out issues with MySQL lagging behind (even IF it did, that shouldn't give me this weirdness, but I wanted to make sure.
Looking at the code (thank you, Owen Winkler, for the XRef at redalt.com!) doesn't give me the impression that those ID's are cached somewhere, so right now I'm stuck.
Am I missing something? Has anyone seen this behaviour? And most important: how the hell am I going to solve this?